【问题标题】:Change file permission after FTP UploadFTP上传后更改文件权限
【发布时间】:2020-07-04 10:02:50
【问题描述】:

我正在将桌面文件夹中的文件上传到 Windows Server 2012 服务器。
上传正常,但我需要更改上传文件的读取和删除权限。
我怎样才能在这段代码中做到这一点?

    string ftpIPServidor = "XXXX"; 
    string ftpUsuarioID = "XX";
    string ftpSenha = "XXXXXXX";

    FileInfo _arquivoInfo = new FileInfo(_nomeArquivo);
    string uri = "ftp://" + ftpIPServidor + "/" + _arquivoInfo.Name;
    FtpWebRequest requisicaoFTP;
    requisicaoFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpIPServidor + "/" + _arquivoInfo.Name));

    requisicaoFTP.Credentials = new NetworkCredential(ftpUsuarioID, ftpSenha);

    requisicaoFTP.KeepAlive = false;

    requisicaoFTP.Method = WebRequestMethods.Ftp.UploadFile;

    requisicaoFTP.UseBinary = true;

    requisicaoFTP.ContentLength = _arquivoInfo.Length;

    // Define o tamanho do buffer para 2kb
    int buffLength = 2048;
    byte[] buff = new byte[buffLength];
    int _tamanhoConteudo;

    FileStream fs = _arquivoInfo.OpenRead();
    var horaAgora = DateTime.Now;

    try
    {
        Stream strm = requisicaoFTP.GetRequestStream();

        _tamanhoConteudo = fs.Read(buff, 0, buffLength);

        while (_tamanhoConteudo != 0)
        {
            // Escreve o conteudo a partir do arquivo para o stream FTP 
            strm.Write(buff, 0, _tamanhoConteudo);
            _tamanhoConteudo = fs.Read(buff, 0, buffLength);
        }

        strm.Close();
        fs.Close();

        Console.WriteLine(horaAgora + " :> Upload of " + _arquivoInfo.Name);
        fi.Delete();
    }
    catch (Exception ex)
    {
        Console.WriteLine(horaAgora + " :> Err " + _arquivoInfo.Name);

    }

【问题讨论】:

    标签: c# .net-core windows-server-2012


    【解决方案1】:

    在包System.IO.FileSystem.AccessControl中有名为GetAccessControlSetAccessControl的扩展方法

    更多信息在这里

    How to modify file access control in .NET Core

    【讨论】:

      猜你喜欢
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 2015-09-04
      • 2013-02-14
      • 2017-07-16
      • 2015-06-16
      • 1970-01-01
      相关资源
      最近更新 更多