首页 > 解决方案 > FTP上传后更改文件权限

问题描述

我正在将桌面文件夹中的文件上传到 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-corewindows-server-2012

解决方案


包中有调用的扩展方法GetAccessControlSetAccessControlSystem.IO.FileSystem.AccessControl

更多信息在这里

如何在 .NET Core 中修改文件访问控制


推荐阅读