调试模式下创建webpart页面错误

发布于 2024-12-04 11:35:07 字数 1583 浏览 1 评论 0原文

我实际上正在创建一个网页部件页面并使用以下帖子中的这种技术 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难忘№最初的完美 2024-12-11 11:35:07

显然,该错误并不表明您是否有权访问 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();
}

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();
}
薔薇婲 2024-12-11 11:35:07

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

It sounds like your application pool does not have access to your 12hive path location.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文