2013-10-30 27 views
0

如果應用程序在與targetpath相同的域中工作,我的代碼的這部分工作。 但我需要應用程序在域外工作,並且可以將文件複製到域內的文件夾。將文件上傳到域內的文件夾,域名外的用戶

當我嘗試上傳文件不起作用時,因爲文件夾訪問需要用戶和密碼才能訪問該文件夾。 如果我嘗試打開域外的目標路徑,Windows會要求輸入憑據。

那麼,我怎麼能通過代碼的用戶和密碼?

private void btnAnexar_Click(object sender, EventArgs e) 
    { 
     String input = string.Empty; 
     OpenFileDialog dialog = new OpenFileDialog(); 

     dialog.Filter ="All files (*.*)|*.*"; 

     dialog.InitialDirectory = "C:"; 
     dialog.Title = "Select a text file"; 

     if (dialog.ShowDialog() == DialogResult.OK) 
      input = dialog.FileName; 

     try 
     { 
      string fileName = dialog.SafeFileName; 
      string extension = Path.GetExtension(fileName); 
      string sourcePath = Path.GetDirectoryName(input); 
      string sourceFile = System.IO.Path.Combine(sourcePath, fileName); 

      copia = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + extension; 

      MakeUnique(targetPath + "\\"+ copia); 

      string destFile = System.IO.Path.Combine(targetPath, copia); 


      if (!System.IO.Directory.Exists(targetPath)) 
      { 
       System.IO.Directory.CreateDirectory(targetPath); 
      } 

      System.IO.File.Copy(sourceFile, destFile, true); 
      linkLabel1.Text = "...\\" + copia; 

      if (input == String.Empty) 
       return; //user didn't select a file to open 
     } 
     catch (Exception e3) 
     { 
      Console.WriteLine(e3.ToString()); 
     } 
    } 

回答

0

的用戶,您需要下運行的程序有寫權限到目標文件夾。您需要成爲具有更改目標目錄讀/寫權限權限的用戶,或者讓此類用戶在代碼中執行此操作的憑證。

+0

感謝您的幫助。但是我已經在文件夾中插入了完全控制權限,但是如果我嘗試通過域外的用戶訪問該文件夾,它仍然要求憑據。 – Pepper

+0

完全控制到「每個人」 – Pepper