0

如果应用程序与目标路径在同一个域中工作,我的这部分代码就可以工作。但是我需要应用程序在域外工作,并且可以将文件复制到域内的文件夹中。

当我尝试上传文件时不起作用,因为文件夹访问需要用户和密码才能访问文件夹。如果我尝试在域外打开目标路径,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());
        }
    }
4

2 回答 2

0

您正在运行程序的用户需要对目标文件夹具有写入权限。您需要成为有权更改目标目录的读/写权限的用户,或者拥有此类用户在代码中执行此操作的凭据。

于 2013-10-30T20:11:34.273 回答
0

您可以使用UploadFiles.On.External.Domain块包:

  1. 配置服务:
services.AddSingleton<IUpLoadFile, UpLoadFile>();
  1. 在 appsettings.json 中写下这段代码:
"UploadFileSettings": {
    "Domain": "yourdomain.com",  
    "Root": "root//www//files, 
    "Separator":"//", 
    "MaximumSize": 5 
},
"Domain": "your domain name",
"Root": "your server root", 
"Separator":"{for Windows //}, {for Linux \\}",
"MaximumSize":  "number by MB.",

  1. 在构造函数中注入 IUpLoad:
private readonly IUpLoadFile UpLoadFile; 
public testController(IUpLoadFile iUpLoadFile)  { UpLoadFile = upLoadFile; } 
  1. 使用函数
     I- UpLoadFile.UpLoad(); 
    II- UpLoadFile.UpLoad2(); 
   III- UpLoadFile.Delete();
于 2022-02-22T04:42:06.433 回答