调试模式下创建webpart页面错误
我实际上正在创建一个网页部件页面并使用以下帖子中的这种技术 Alex Angas 在 以编程方式实例化 Sharepoint 中的 Web 部件页面
string SiteLocation = "http://abcd.com/sites/forum/";
SPSecurity.RunWithElevatedPrivileges(delegate(){
using(SPSite site = new SPSite(SiteLocation)){
using(SPWeb web = site.OpenWeb()){
foreach(SPWeb oweb in web.Webs){
bool allowUnsafeUpdates = oWeb.AllowUnsafeUpdates;
oWeb.AllowUnsafeUpdates = true;
string strFileName = "Mobile.aspx";
string strTemplateFileName = "spstd1.aspx";
string strPath = "TEMPLATE\\1033\\STS\\DOCTEMP\\SMARTPGS";
string hive = SPUtility.GetGenericSetupPath(strPath);
//--- Error encountered on this line ---
FileStream stream = new FileStream(hive + strTemplateFileName,FileMode.Open);
//--------------------------------------
SPFolder libraryFolder = oWeb.GetFolder(WebPartPageDocLibName);
SPFileCollection files = libraryFolder.Files;
SPFile newFile = files.Add(strFileName, stream);
oWeb.Update();
oWeb.AllowUnsafeUpdates = allowUnsafeUpdates;
}
}
}
});
I遇到此错误
IOException 未由用户代码处理
进程无法访问文件 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\1033\STS\DOCTEMP\SMARTPGS\spstsd1.aspx'
但令我恼火的是,当我在调试模式下进入它时,错误不会出现。当我重新启动应用程序并运行它而不踩到它时,错误就会出现。有人可以帮我解决这个问题吗?
I'm actually creating a webpart page and use this technique from the post of
Alex Angas in Programmatically instantiate a web part page in Sharepoint
string SiteLocation = "http://abcd.com/sites/forum/";
SPSecurity.RunWithElevatedPrivileges(delegate(){
using(SPSite site = new SPSite(SiteLocation)){
using(SPWeb web = site.OpenWeb()){
foreach(SPWeb oweb in web.Webs){
bool allowUnsafeUpdates = oWeb.AllowUnsafeUpdates;
oWeb.AllowUnsafeUpdates = true;
string strFileName = "Mobile.aspx";
string strTemplateFileName = "spstd1.aspx";
string strPath = "TEMPLATE\\1033\\STS\\DOCTEMP\\SMARTPGS";
string hive = SPUtility.GetGenericSetupPath(strPath);
//--- Error encountered on this line ---
FileStream stream = new FileStream(hive + strTemplateFileName,FileMode.Open);
//--------------------------------------
SPFolder libraryFolder = oWeb.GetFolder(WebPartPageDocLibName);
SPFileCollection files = libraryFolder.Files;
SPFile newFile = files.Add(strFileName, stream);
oWeb.Update();
oWeb.AllowUnsafeUpdates = allowUnsafeUpdates;
}
}
}
});
I encounter this error
IOException was unhandled by user code
The process cannot access the file 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\1033\STS\DOCTEMP\SMARTPGS\spstsd1.aspx'
but what irritates me is that when I step into it in debugging mode, the error won't appear. And when I restart the application and run it without stepping on to it, the error comes out. Can someone help me on this please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,该错误并不表明您是否有权访问 12hive 路径。如果您检查代码,您实际上打开了 spstd1.aspx 文件,并且由于操作系统不允许在打开该文件时使用该文件,因此您会在运行时遇到此类错误。
如您所知,在调试会话期间,您没有遇到此错误的原因是它为您的流提供了足够的时间来完成该过程。
您可以通过正确处理 FileStream 对象来解决此问题。
Obviously the error does not point to whether you have the access to your 12hive path or not. If you examine your code, you actually opened the spstd1.aspx file and because the operating system does not allow usage of the file when it is open, that's why you hit this kind of error during run time.
As you know, during debugging session the reason why you did not hit this error is because it gives your stream enough time to finish the process.
You can solve this issue by properly disposing you FileStream object.
听起来您的应用程序池无权访问您的 12hive 路径位置。
It sounds like your application pool does not have access to your 12hive path location.