2016-06-21 26 views
4

當我嘗試使用C#程序將本地文件複製到Sharepoint服務器時,我有一個奇怪的行爲,它使用Sharepoint提供的UNC路徑訪問文件系統。首先,我的用戶具有訪問特定Sharepoint文件夾所需的全部權限。將文件複製到Sharepoint共享失敗,除非用戶第一次連接到Sharepoint服務器

這是我的操作基本上是這樣的:

string targetSharepointPath = @"\\[email protected]\DavWWWRoot\team\wmscompanydep\Software Releases\MyToolConfig" 
System.IO.File.Copy(sourcePath, targetSharepointPath, false); 

這種失敗並顯示錯誤「網絡路徑沒有被發現。」

只要我複製上面的路徑並粘貼到WIndows文件資源管理器(而不是 Internet Explorer,這只是一個UNC路徑),一切正常。

所以我的假設是,在後臺,Windows資源管理器做了一點點。但是什麼?我不必輸入任何憑據,targetSharepointPath只需在資源管理器中工作,只要輸入一次,它也可以在我的C#程序中使用。在我重新啓動系統之前,我必須重複該步驟。爲什麼,以及如何以編程方式實現這一目標?我常常在「普通」Windows服務器上使用UNC路徑,一旦用戶擁有權限,我不需要任何額外的身份驗證。

回答

5

要連接到Sharepoint,您需要一個名爲WebClient的窗口服務。

當您從資源管理器中打開該鏈接時,它將確保該服務已啓動。這可能是您在瀏覽器中打開鏈接後能夠從您的應用訪問Sharepoint的原因。

您可以確保您的客戶有service自動啓動實現它。

或者您可以嘗試以這種方式從您的代碼啓動服務。 (您可能需要管理員權限此)

using (ServiceController service= new ServiceController("WebClient")) 
    { 

     if (service.Status == ServiceControllerStatus.Stopped) 
     { 

      service.Start(); 
      service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 15)); 
      //Check status here by calling service.Status and proceed with your code. 
     } 
     else 
     { 
      //proceed with your code as the service is up and running 
     } 
    } 
+2

我認爲它應該工作... + 1 – Ansari

+0

我現在用了幾次,和它的工作,很好的答案,謝謝! – Erik

+1

這正是我所需要的。我得到一個網絡路徑找不到錯誤。關於上面的代碼的一個注意事項是它需要對System.ServiceProcess.dll的引用。 https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller(v=vs.110).aspx –

相關問題