简体   繁体   中英

Edit a text file over FTP in C#?

I want to, basically, use the following code to edit a C# file:

var file = new StreamReader("ftp://xxx.xxx.x.x/xxx.txt"); //[ip address/file]
        label1.Text = file.ReadLine();
        file.Close();
        var fw = new StreamWriter("ftp://xxx.xxx.x.x/xxx.txt"); //[ip address/file]
        fw.WriteLine(textBox1.Text);
        fw.Close();

But it doesn't work, how do I do this?

Edit file via FTP is:

  1. Download file
  2. Edit file locally (in memory)
  3. Upload file

For steps 1 and 3 check this .

The FtpWebRequest seems very complex compared to the ftplib library @ http://ftplib.codeplex.com/

Here is their example...

   using (FtpConnection ftp = new FtpConnection("ftpserver", "username", "password"))
   {

   ftp.Open(); /* Open the FTP connection */
   ftp.Login(); /* Login using previously provided credentials */

   if (ftp.DirectoryExists("/incoming")) /* check that a directory exists */
       ftp.SetCurrentDirectory("/incoming"); /* change current directory */

   if (ftp.FileExists("/incoming/file.txt"))  /* check that a file exists */
       ftp.GetFile("/incoming/file.txt", false); /* download /incoming/file.txt as file.txt to current executing directory, overwrite if it exists */

   //do some processing

   try
   {
       ftp.SetCurrentDirectory("/outgoing");
       ftp.PutFile(@"c:\localfile.txt", "file.txt"); /* upload c:\localfile.txt to the current ftp directory as file.txt */
   }
   catch (FtpException e)
   {
       Console.WriteLine(String.Format("FTP Error: {0} {1}", e.ErrorCode, e.Message));
   }

   foreach(var dir in ftp.GetDirectories("/incoming/processed"))
   {
       Console.WriteLine(dir.Name);
       Console.WriteLine(dir.CreationTime);
       foreach (var file in dir.GetFiles())
       {
           Console.WriteLine(file.Name);
           Console.WriteLine(file.LastAccessTime);
       }

查看.NET 4.0中包含的FtpWebRequest类来帮助解决这个问题。

As i see i`ma little late, but i faced the same problem like you, and after i dig for a solution, i found a way to do the changes without downloading, and uploading the file.

My goal was to save all the changes made during the program execution at the closing, and to load these data at program start from a .txt file stored in a FTP server.

Here is the two methods are used:

public static void Save(ArrayList dataStore)
    {
        WebClient request = new WebClient();
        string url = "ftp://ftpname/directory/" + "file.txt";
        request.Credentials = new NetworkCredential("username", "password");
        Stream postStream = request.OpenWrite(url);

        foreach (Data data in dataStore)
        {
            byte[] writeData = Encoding.ASCII.GetBytes(data + "#");
            postStream.Write(writeData, 0, writeData.Length);
        }
    }

public static ArrayList Load()
    {
        ArrayList dataStore = new ArrayList();

        WebClient request = new WebClient();
        string url = "ftp://ftpname/directory/" + "file.txt";
        request.Credentials = new NetworkCredential("username", "password");

        byte[] newFileData = request.DownloadData(url);
        string fileString = Encoding.UTF8.GetString(newFileData);

        if (fileString == "")
        {
            return dataStore;
        }

        string[] dataString = fileString.Split('#');

        foreach (string data in dataString)
        {
            if (data != "")
            {
                dataStore.Add(data);
            }
        }

        return dataStore;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM