在 C# 中通过 FTP 编辑文本文件?

发布于 2024-10-17 12:10:06 字数 357 浏览 2 评论 0原文

基本上,我想使用以下代码来编辑 C# 文件:

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

但它不起作用,我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨离汐 2024-10-24 12:10:06

通过 FTP 编辑文件的方法是:

  1. 下载文件
  2. 在本地(在内存中)编辑文件
  3. 上传文件

对于步骤 1 和 3,请检查 这个

Edit file via FTP is:

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

For steps 1 and 3 check this.

不忘初心 2024-10-24 12:10:06

与 ftplib 库相比,FtpWebRequest 看起来非常复杂@ http://ftplib.codeplex.com/

这是他们的例子...

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

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);
       }
久而酒知 2024-10-24 12:10:06

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

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

我早已燃尽 2024-10-24 12:10:06

正如我所看到的,我有点晚了,但我遇到了和你一样的问题,在我寻找解决方案之后,我找到了一种无需下载和上传文件即可进行更改的方法。

我的目标是在程序结束时保存程序执行期间所做的所有更改,并在程序启动时从存储在 FTP 服务器中的 .txt 文件加载这些数据。

下面是使用的两种方法:

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

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文