2011-09-02 75 views
0

在我的SharePoint應用程序頁面,我試圖將文件從網絡共享文件夾複製。而我的代碼的下面一樣..遠程文件從SharePoint應用程序訪問頁面

try 
    { 
     File.Copy("\\MShare\Public\Test.txt", "C:\Temp\Test.txt", true); 
     LblMessage.Text = "File copied."; 
    } 
    catch (Exception ex) 
    { 
     LblMessage.Text = ex.ToString() + " - " + ex.Message; 
    } 

,如果我它的工作好測試在ASP.NET網站相同的代碼。但我收到提示與SP應用程序頁面遵循..

System.UnauthorizedAccessException: Access to the path '\\MShare\Public\Test.txt' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite) at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) at TestApp.PullingFile.ButGet_Click(Object sender, EventArgs e) - Access to the path '\\MShare\Public\Test.txt' is denied. 

我試着按照this後..不..工作和 實現模擬我嘗試通過更改web.config與<trust level="WSS_Medium" originUrl="" />也.. ..

回答

0

默認情況下SharePoint使用impersonating。意味着您的代碼在當前用戶的憑據下運行。該用戶沒有(也不應該有)訪問服務器的文件系統。

你可以做的是revert to the system accounts credentials爲了訪問文件系統:

SPSecurity.RunWithElevatedPrivileges(() => 
{ 
    File.Copy("\\MShare\Public\Test.txt", "C:\Temp\Test.txt", true); 
}); 

你有什麼要牢記:

  1. 系統帳戶(應用程序池帳戶)具有不一定要訪問文件系統(最少權限方案)。 (SO question
  2. 系統帳戶(應用程序池帳戶)不一定訪問網絡共享。
  3. 訪問您的應用程序頁面的任何用戶都可以執行您的文件複製代碼。你必須自己關心授權。

最後但並非最不重要
爲什麼你必須將文件複製到服務器的文件系統畢竟?你是否需要在服務器上進行物理上的操作,或者這只是暫時的(正如通過路徑猜測的那樣)。