gpt4 book ai didi

c# - ftp 损坏 exe 和 dll 文件 (C#)

转载 作者:行者123 更新时间:2023-12-02 22:28:39 25 4
gpt4 key购买 nike

我有以下代码用于从服务器下载文件,适用于文本文件。代码取自 MSDN 示例:

public void DownloadFile(string serverPath, string localPath)
{

try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + serverPath);

request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(_domain + "\\" + _username, _password);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string contents = reader.ReadToEnd();
File.WriteAllText(localPath, contents);

reader.Close();
response.Close();

}
catch (WebException ex)
{
string exMsg = string.Empty;


//add more error codes
FtpWebResponse response = (FtpWebResponse)ex.Response;
MessageBox.Show(response.StatusCode.ToString());

switch(response.StatusCode) {
case FtpStatusCode.NotLoggedIn:
exMsg = "wrong password";
break;

case FtpStatusCode.ActionNotTakenFileUnavailable:
exMsg = "file you are trying to load is not found";
break;

default:
exMsg = "The server is inaccessible or taking too long to respond.";
break;
}

throw new Exception(exMsg);
}

return;
}

但是,它会破坏 dll 和 exe。知道这里的罪魁祸首是什么吗?

最佳答案

试试看:

request.UseBinary = true;

关于c# - ftp 损坏 exe 和 dll 文件 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12606424/

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