开发者

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);
       }


Check out the FtpWebRequest class that is included in .NET 4.0 to help with this.


As i see i`m a 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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜