2

I am uploading files in a folder on the desktop to the Windows Server 2012 server.
The upload is proceeding correctly, but I need to change the read and delete permission of the uploaded file.
How can I do this in this code?

    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);

    }
Mikail hP
  • 29
  • 4

1 Answers1

2

There are extension methods called GetAccessControl and SetAccessControl in the package System.IO.FileSystem.AccessControl

More info here

How to modify file access control in .NET Core

Rosco
  • 2,108
  • 17
  • 17