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.
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.
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();
}