如何组合我的FTP查询?

时间:2012-12-06 18:04:52

标签: c# ftp

我的程序多次查询FTP服务器以读取和上传信息。如何将所有这些组合成一个FTP类来处理所有内容?

    private static void UploadToFTP(string[] FTPSettings)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPSettings[0]);
            request.Method = WebRequestMethods.Ftp.MakeDirectory;

            request.Credentials = new NetworkCredential(FTPSettings[1], FTPSettings[2]);

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        }
        catch
        {
        }

        try 
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPSettings[0] + Path.GetFileName(file));
            request.Method = WebRequestMethods.Ftp.UploadFile;

            request.Credentials = new NetworkCredential(FTPSettings[1], FTPSettings[2]);

            StreamReader source = new StreamReader(file);
            byte[] fileContents = Encoding.UTF8.GetBytes(source.ReadToEnd());
            source.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            response.Close();

            RegenLog();
        }
        catch (Exception e)
        {
            File.AppendAllText(file, string.Format("{0}{0}Upload Failed - ({2}) - {1}{0}", nl, System.DateTime.Now, e.Message.ToString()));
        }
    }

    private static void CheckBlacklist(string[] FTPSettings)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPSettings[0] + "blacklist.txt");
            request.Credentials = new NetworkCredential(FTPSettings[1], FTPSettings[2]);

            using (WebResponse response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (TextReader reader = new StreamReader(stream))
                    {
                        string blacklist = reader.ReadToEnd();

                        if (blacklist.Contains(Environment.UserName))
                        {
                            File.AppendAllText(file, string.Format("{0}{0}Logger terminated - ({2}) - {1}{0}", nl, System.DateTime.Now, "Blacklisted"));
                            uninstall = true;
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            File.AppendAllText(file, string.Format("{0}{0}FTP Error - ({2}) - {1}{0}", nl, System.DateTime.Now, e.Message.ToString()));
        }
    }

    private static void CheckUpdate(string[] FTPSettings)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPSettings[0] + "update.txt");
            request.Credentials = new NetworkCredential(FTPSettings[1], FTPSettings[2]);

            using (WebResponse response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (TextReader reader = new StreamReader(stream))
                    {
                        string newVersion = reader.ReadToEnd();

                        if (newVersion != version)
                        {
                            update = true;
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            File.AppendAllText(file, string.Format("{0}{0}FTP Error - ({2}) - {1}{0}", nl, System.DateTime.Now, e.Message.ToString()));
        }
    }

我知道我的代码也有点不一致和混乱,但这是我第一次在C#中使用FTP。请给出任何建议!

0 个答案:

没有答案