1

我想创建一个 TcpClient ,它会按名称自动从服务器获取多个文件。

我想了解如何构建这样的应用程序。

我的想法是:

制作一个包含 SwitchCase 的 for 循环,我在其中指定我的文件名。我真的不知道这是否会奏效。

要退出 for 循环,我可以将索引运算符与文件数进行比较。如果它们相等,那么我将退出 for 循环。

我的想法的例子:

for (int i = 1; i <= 4; i++)
{

switch (----)
        {
            case 'file1':
                code...
                break;
            case 'file2':
                code...
            case 'file3':
                code...
                break;
            case 'file4':
                code...
                break;
            default:
                code...
                break;
        }

}
4

2 回答 2

2

要使用 ftp 下载文件,您可以使用FtpWebRequest并且对于 http 使用HttpWebRequest。下面是一个简单的例子,说明如何使用 http 请求文件(方法与 ftp 类似):

public void Download(string url, string localPath)
{
    HttpWebRequest request = HttpWebRequest.Create(url);
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    Stream stream = response.GetResponseStream();
    FileStream fs = new FileStream(localPath, FileMode.Create);

    int count;
    byte[] buffer = new byte[8096];
    while ((count = stream.Read(buffer, 0, 8096)) > 0)
        fs.Write(buffer, 0, count);

    fs.Dispose();
    response.Close();
}

您应该迭代一个数组,而不是在 for 循环中使用开关:

string[] files = new string[]{ url1, url2, ...};
for(int i = 0; i < files.Length; i++)
{
    Download(files[i], "file" + i);
}
于 2013-10-18T09:27:57.013 回答
1

我这样解决了:

我的应用程序。从服务器获取 2 个文件并移动文件并重命名它们。

测试 = mytest test111 = test2

    static string myfile1 = @"C:\inbox\mytest.txt";
    static string myfile2 = @"C:\inbox\test2.txt";
    //files from server        
    static string myServerfile = @"C:\Users\me\Documents\file_client\bin\Debug\test.csv";
    static string myServerfile1 = @"C:\Users\RH-T3\Documents\file_client\bin\Debug\test111.txt";


 public static void Main(string[] args)
    {   
            try
            {
                for (int i = 0; i < 2; i++)
                {
                    if (i == 0)
                    {
                        Console.WriteLine("Downloading test.csv");
                        string fileName = "test.csv";
                        Console.WriteLine("Client starts...");
                        //args[0] = Console.ReadLine();
                        file_client client = new file_client(args);
                        Console.WriteLine("efter file_client...");
                        NetworkStream serverStream = client.clientSocket.GetStream();
                        LIB.writeTextTCP(serverStream, fileName);
                        long rest = long.Parse(LIB.readTextTCP(serverStream));

                        byte[] inStream = new byte[rest];

                        while (rest != 0)
                        {
                            rest = rest - serverStream.Read(inStream, 0, inStream.Length);
                            Console.WriteLine("REST: " + rest);

                        }
                        FileStream fs = new FileStream(fileName,                            FileMode.Create);
                        fs.Write(inStream, 0, inStream.Length);
                        {
                            fs.Close();
                            serverStream.Close();
                        }
                        if (File.Exists(myfile1))
                        {
                            File.Delete(myfile1);
                        }
                        File.Move(myServerfile, myfile1);

                        Console.WriteLine("Moved");
                        System.Threading.Thread.Sleep(500);
                    } 
                     else  
                       {
                        Console.WriteLine("Downloading .txt file");
                        string fileName = "test111.txt";
                        Console.WriteLine("Client starts...");
                        //args[0] = Console.ReadLine();
                        file_client client = new file_client(args);
                        Console.WriteLine("efter file_client...");
                        NetworkStream serverStream = client.clientSocket.GetStream();
                        LIB.writeTextTCP(serverStream, fileName);
                        long rest = long.Parse(LIB.readTextTCP(serverStream));
                        byte[] inStream = new byte[rest];
                        while (rest != 0)
                        {
             rest = rest - serverStream.Read(inStream, 0, inStream.Length);
                            Console.WriteLine("REST: " + rest);
                        }
                        FileStream fs = new FileStream(fileName,                            FileMode.Create);
                        fs.Write(inStream, 0, inStream.Length);
                        {
                            fs.Close();
                            serverStream.Close();
                        }
                        if (File.Exists(myfile2))
                        {
                            File.Delete(myfile2);
                        }
                        File.Move(myServerfile1, myfile2);

                        Console.WriteLine("Moved");
                        System.Threading.Thread.Sleep(500);
                    }
                }
        }
                catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine("Cannot be DONE!");
            } 
于 2013-10-25T07:27:58.677 回答