【问题标题】:Edit text file residing on Network (LAN)编辑驻留在网络 (LAN) 上的文本文件
【发布时间】:2013-03-20 11:07:46
【问题描述】:

我想从网络上的 c# 应用程序访问和编辑文本文件,并通过 ip 地址访问。访问文件不需要凭据,因此我需要一些示例代码或指南,了解如何开始完成此任务。我要编辑的文件在 Android 平板电脑上,并且我通过 ip 地址访问的 android 应用程序(SD 卡)的存储,因为我的机器和 Android 设备在同一个网络上。 谢谢

  try
        {
        StreamReader sr = new StreamReader("192.168.1.2/NewFolder/TickerText.txt");

            //Read the first line of text
            line = sr.ReadLine();

            //Continue to read until you reach end of file
            while (line != null) 
            {
                //write the lie to console window
                textBox1.Text=line;
                //Read the next line
                line = sr.ReadLine();
            }

            //close the file
            sr.Close();
            Console.ReadLine();
        }
        catch(Exception ae)
        {
            textBox1.Text = ae.Message;
        }

【问题讨论】:

  • 你写过代码来访问本地文件吗?您在访问网络文件时遇到特定问题吗?你需要告诉我们你需要知道什么。
  • 这并没有显示出任何类型的研究工作。 -1
  • 可以在两台机器之间建立SMB连接吗?您是否能够创建应用程序来编辑本地文件?如果 2x 是:使用相同的技术远程编辑文件。
  • 我可以编辑驱动器上的本地文件。我想通过 ftp 编辑文件,通过 ip 地址访问文件夹,因为它在网络上。

标签: c# .net winforms text-files


【解决方案1】:

假设两台电脑都是windows,你可以共享文件所在的文件夹并访问它以进行写入:

using (StreamWriter writer = new StreamWriter(@"\\IPADDRESS\directories\file.txt") 
{
    writer.Write("Word ");
}

阅读使用:

File.ReadAllLines(@"\\IPADDRESS\directories\file.txt");

与任何其他文件读取器/写入器相同。


附加信息涉及文件所在的 android 操作系统,以及包含 FTP 我只能说

对你有用。

我会粘贴CodeProject article的相关信息。
这些代码都不是我的,但它会满足您的要求。
完成后请记住将文件上传回安卓系统。

/* Download File */
    public void download(string remoteFile, string localFile)
    {
        try
        {
            /* Create an FTP Request */
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
            /* Log in to the FTP Server with the User Name and Password Provided */
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            /* When in doubt, use these options */
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            /* Specify the Type of FTP Request */
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            /* Establish Return Communication with the FTP Server */
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            /* Get the FTP Server's Response Stream */
            ftpStream = ftpResponse.GetResponseStream();
            /* Open a File Stream to Write the Downloaded File */
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            /* Buffer for the Downloaded Data */
            byte[] byteBuffer = new byte[bufferSize];
            int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
            /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
            try
            {
                while (bytesRead > 0)
                {
                    localFileStream.Write(byteBuffer, 0, bytesRead);
                    bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            /* Resource Cleanup */
            localFileStream.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    }

【讨论】:

  • 感谢您的快速响应,实际上该文件位于 Android 平板电脑上,我正在使用适用于 android 的应用程序在我的系统上进行 ftp 控制。系统和平板电脑存储在同一网络上
  • @JibranKhan 您应该使用该信息编辑您的问题,知道这一点非常重要,每个人都会因为 c# / .net 假设您使用 2 台 Windows 机器,说我还没有知道如何从 windows 系统访问 android 系统上的文件。
  • @JibranKhan 我非常有信心 CodeProject C# ftp 链接将是您所需要的:)
  • 感谢您的帮助,我会尝试一下,似乎正是我想要的。
  • 谢谢@Serdalis,我已经成功实现了我想要的。再次感谢您。
【解决方案2】:

你可以像这样读取文件:

File.ReadAllLines(@"\\192.168.1.1\FileName.txt");

这会给你一个string[],或者像这样:

foreach (string line in File.ReadLines(@"\\192.168.1.1\FileName.txt"))
{
    ...
}

一行一行。您可以像这样附加到文件(例如新行):

File.AppendAllText(@"\\192.168.1.1\FileName.txt", "Hello, World!");

还有很多其他方法。利用 System.Io 命名空间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2010-10-12
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    相关资源
    最近更新 更多