Edit text file via 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 does not work, how can I do it?

+3
source share
4 answers

Edit file via FTP:

  • Download file
  • Edit file locally (in memory)
  • Upload file

For steps 1 and 3, check this .

+5
source

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

Here is an 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);
       }
+2
source

FtpWebRequest, .NET 4.0, .

+1

, , , , , , .

My goal was to save all the changes made during program execution at closing, and to load this data when the program was launched from the .txt file stored on the FTP server.

Here are two methods:

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
source

Source: https://habr.com/ru/post/1791884/


All Articles