0

我想从网络上并通过 IP 地址访问的 c# 应用程序访问和编辑文本文件。访问文件不需要凭据,因此我需要一些示例代码或指南,了解如何开始完成此任务。我要编辑的文件在 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;
        }
4

2 回答 2

3

假设两台计算机都是 Windows,您可以共享文件所在的文件夹并使用以下方法访问它进行写入:

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

和阅读使用:

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

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


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

会对你有用。

我将粘贴CodeProject 文章中的相关信息。
这段代码都不是我的,但它会做你需要的。
完成后请记住将文件上传回android系统。

/* 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;
    }
于 2013-03-20T11:11:49.293 回答
1

您可以读取这样的文件:

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命名空间。

于 2013-03-20T11:12:53.873 回答