2013-10-30 25 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