【问题标题】:Getting error in C# trying to open file from SharePoint server (FileInfo/FileStream)在 C# 中尝试从 SharePoint 服务器打开文件时出错 (FileInfo/FileStream)
【发布时间】:2018-01-19 09:09:02
【问题描述】:

我在 SharePoint 中有一项功能,可将 Web 部件添加到所创建的每个新网站的 Web 部件库中。有问题的代码行如下:

string webPartPath = System.Web.Hosting.HostingEnvironment.MapPath("~/" + 
CompanyConstants.CustomWebPart);
FileInfo f = new FileInfo(webPartPath);          
FileStream s = f.Open(FileMode.Open, FileAccess.Read);

字符串 webPartPath 的计算结果如下:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\template\layouts\*CENSORED*\*CENSORED*\WebPart\CustomWebpart.dwp

我收到的错误如下:

该进程无法访问文件“C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\template\layouts*CENSORED**CENSORED*\WebPart\CustomWebpart.dwp”,因为它正被另一个进程。

有谁知道为什么会发生这种情况?非常感谢您的帮助。

【问题讨论】:

    标签: c# sharepoint


    【解决方案1】:

    我可以用这种方式复制问题:

      String webPartPath = @"c:\test\temp.html";
      FileInfo f = new FileInfo(webPartPath);
      FileStream s = f.Open(FileMode.Open, FileAccess.Read);
    
      FileInfo f2 = new FileInfo(webPartPath);
      FileStream s2 = f.Open(FileMode.Open, FileAccess.Read);
    

    这让我相信你没有清理你正在创建的流。

    将您的代码改为此(如下)。所有代码都应该在 USING 中。这将确保流关闭(即使有错误)。任何继承 IDisposable 的对象都可以通过这种方式调用。

    using (FileStream s = f.Open(FileMode.Open, FileAccess.Read))
    {
        // Put your file reading code in here.
    }
    

    我还建议在更改代码后重新启动 IIS,因为即使在更改之后,您也可能在之前的执行中发生锁定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 2013-02-06
      相关资源
      最近更新 更多