2013-03-20 19 views
0

我想訪問和編輯來自網絡上的c#應用程序的文本文件,並通過ip地址訪問。沒有證書需要訪問文件,所以我想要一些示例代碼或指導我如何才能開始實現此任務。我想要編輯的文件位於Android平板電腦和我通過IP地址訪問的Android應用程序(SD卡)的存儲上,因爲我的機器和Android設備位於同一網絡中。 感謝編輯位於網絡(LAN)上的文本文件

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

您是否編寫了訪問本地文件的代碼?你有訪問網絡文件的特定問題嗎?你需要告訴我們你需要知道什麼。 – 2013-03-20 11:13:09

+1

這沒有顯示任何類型的研究工作。 -1 – Otiel 2013-03-20 11:14:02

+0

您可以在兩臺機器之間建立SMB連接嗎?你能夠創建一個應用程序來編輯本地文件嗎?如果2x是:使用相同的技術來遠程編輯文件。 – Simon 2013-03-20 11:20:37

回答

3

假設兩臺計算機的窗口,您可以共享該文件和訪問它使用書寫的文件夾:

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

和使用閱讀:

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

與其他文件讀取器/寫入器相同。


與涉及Android操作系統的文件上,而FTP列入額外的信息,我只能說,

將對您有用。

我會粘貼來自CodeProject article的相關信息。
這些代碼都不是我的,但它會做你想要的。
只要記住一旦完成,就將文件上傳回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; 
    } 
+0

感謝您的快速響應,實際上該文件是在Android平板電腦上,我正在使用android的應用程序在我的系統上擁有它的ftp控件。系統和平板電腦存儲在同一網絡上 – 2013-03-20 11:28:41

+0

@JibranKhan你應該用這些信息來編輯你的問題,知道這一點非常關鍵,每個人都會認爲你使用2臺Windows機器是因爲c#/ .net,我說'但還知道如何從Windows系統訪問Android系統上的文件。 – Serdalis 2013-03-20 11:31:52

+1

@JibranKhan我相當相信,CodeProject C#的ftp鏈接將正是你所需要的:) – Serdalis 2013-03-20 11:41:00

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