【问题标题】:Create a webpart page error during debbugging mode在调试模式期间创建 Web 部件页面错误
【发布时间】:2011-09-15 11:00:49
【问题描述】:

我实际上是在创建一个 webpart 页面并使用来自 亚历克斯·安加斯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'

但让我恼火的是,当我在调试模式下进入它时,错误不会出现。当我重新启动应用程序并在不踩到它的情况下运行它时,就会出现错误。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: c# sharepoint sharepoint-2007


    【解决方案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();
    }
    

    【讨论】:

      【解决方案2】:

      听起来您的应用程序池无法访问您的 12hive 路径位置。

      【讨论】:

      • 如果是这样的话,我如何让我的应用程序池可以访问 12hive 路径?
      猜你喜欢
      • 2023-04-07
      • 2014-05-08
      • 2012-01-15
      • 2023-03-03
      • 1970-01-01
      • 2011-01-05
      • 2012-08-13
      • 2015-09-16
      相关资源
      最近更新 更多