如果应用程序与目标路径在同一个域中工作,我的这部分代码就可以工作。但是我需要应用程序在域外工作,并且可以将文件复制到域内的文件夹中。
当我尝试上传文件时不起作用,因为文件夹访问需要用户和密码才能访问文件夹。如果我尝试在域外打开目标路径,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());
}
}