gpt4 book ai didi

c# - FTP上传后更改文件权限

转载 作者:行者123 更新时间:2023-12-04 10:21:38 26 4
gpt4 key购买 nike

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

}

最佳答案

有一些扩展方法叫做 GetAccessControlSetAccessControl包装内System.IO.FileSystem.AccessControl
更多信息在这里

How to modify file access control in .NET Core

关于c# - FTP上传后更改文件权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60822071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com