在调试模式下创建 webpart 页面错误 [英] Create a webpart page error during debbugging mode

查看:37
本文介绍了在调试模式下创建 webpart 页面错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是在创建一个 webpart 页面,并从帖子中使用此技术Alex Angas 在 在 Sharepoint 中以编程方式实例化 Web 部件页面

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;
        }
    }
}

});

我遇到了这个错误

IOException 未被用户代码处理

进程无法访问文件'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\1033\STS\DOCTEMP\SMARTPGS\spstsd1.aspx'

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.

推荐答案

显然该错误并不指向您是否有权访问您的 12hive 路径.如果您检查您的代码,您实际上打开了 spstd1.aspx 文件,并且由于操作系统不允许在打开时使用该文件,这就是您在运行时遇到此类错误的原因.

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.

您可以通过正确处理 FileStream 对象来解决此问题.

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

这篇关于在调试模式下创建 webpart 页面错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆