我实际上正在创建一个webpart页面,并从帖子中使用此技术 Alex Angas在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;
}
}
}
});
我遇到这个错误
用户代码
未对IOException进行处理进程无法访问文件'C:\ Program Files \ Common Files \ Microsoft Shared \ Web Server Extensions \ 12 \ TEMPLATE \ 1033 \ STS \ DOCTEMP \ SMARTPGS \ spstsd1.aspx'
但令我恼火的是,当我在调试模式中进入它时,错误将不会出现。当我重新启动应用程序并运行它而不踩到它时,错误就出来了。有人可以帮我这个。
答案 0 :(得分:1)
显然,错误并未指出您是否可以访问12hive路径。如果您检查代码,实际上是打开了 spstd1.aspx 文件,并且因为操作系统在打开时不允许使用该文件,这就是您在运行时遇到此类错误的原因。
如您所知,在调试会话期间,您没有遇到此错误的原因是因为它为您的流提供了足够的时间来完成该过程。
您可以通过正确处理FileStream对象来解决此问题。
FileStream stream = null;
try{
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 ---
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;
}
}
}
});
}
catch(Exception ex)
{
// handle or throw your exception
// or do any necessary error handling
throw new Exception(ex.Message,ex);
}
finally{
// it is necessary to dispose your FileStream object to
// allow access of the file spstd1.aspx on the next usage.
if(stream!=null) stream.Dispose();
}
答案 1 :(得分:0)
听起来您的应用程序池无法访问您的12hive路径位置。