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

标签 c# .net-3.5 ftp

我有以下代码用于从服务器下载文件,适用于文本文件。代码取自 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/

相关文章:

c# - Web Api 2 RESTFUL 图片上传

.net - 使 asp.net 应用程序符合 IPv6

.net - 如何提高FtpWebRequest的性能?

c# - 如何进行插值

c# - 从使用原始输入 API 检索的按键集合中获取字符串

sockets - 使用 FTP 命令通过套接字下载文件

laravel - 如何在ovh托管上部署laravel项目?

c# - 为什么这个 AutoMapper 配置不能正确映射?

javascript - 如何: Deserialize a C# object from javascript

c# - 如何在锯齿状数组中查找唯一值