0

I want to access and edit the text file from c# application which is on network and accessed through ip address. There are no credentials required to access the files so i want some sample code or guide on how can i begin to achieve this task. The file i want to edit is on Android Tablet and the storage of the android application (SD card) i am accessing through ip address as my machine and Android device are on same network. thanks

  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;
        }
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
  • 1
    Have you written code to access a local file? Do you have a specific problem with accessing network files? You need to tell us what it is you need to know. – Tim Rogers Mar 20 '13 at 11:13
  • 1
    This does not show any kind of research effort. -1 – Otiel Mar 20 '13 at 11:14
  • Can you establish SMB connections between the two machines? Are you able to create an application to edit a local file? If 2x yes: Use the same techniques to edit the file remotely. – Simon Mar 20 '13 at 11:20
  • I am able to edit the local file on my drive. I want to edit the file over ftp accessing the folders via ip address as it is on network. – Jibran Khan Mar 20 '13 at 11:32

2 Answers2

3

Assuming both computers are windows, you can share the folder that the file is in and access it for writing using:

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

and for reading using:

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

Same as any other file reader / writer.


With the additional information involving the android operating system the file is on, and the inclusion of FTP I can only say that

will be useful to you.

I will paste the relevent information from the CodeProject article.
None of this code is mine, however it will do what you require.
Just remember to upload the file back to the android system once you're done with it.

/* 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;
    }
Community
  • 1
  • 1
Serdalis
  • 10,296
  • 2
  • 38
  • 58
  • Thanks for quick response, actually the file is on Android tablet and i am using an app for android to have its ftp control on my system. The system and tablet storage are on same network – Jibran Khan Mar 20 '13 at 11:28
  • @JibranKhan you should edit your question with that information, it's quite critical to know, every one will assume you are using 2 windows machines because of c# / .net, saying that, I don't yet know how to access a file on an android system from a windows system. – Serdalis Mar 20 '13 at 11:31
  • 1
    @JibranKhan I am quite confident that the CodeProject C# ftp link will be exactly what you need :) – Serdalis Mar 20 '13 at 11:41
  • thanks for your assist, i will try it and seems to be exactly what i wanted. – Jibran Khan Mar 20 '13 at 11:48
  • Thank you @Serdalis, i have successfully achieved what i wanted. Thanks you again. – Jibran Khan Mar 20 '13 at 12:18
  • Code Project code helped to get the job done, for those who want the same can refer to that article – Jibran Khan Mar 20 '13 at 12:19
1

You can read a file like this:

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

and that will give you back a string[], or like this:

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

line by line. And you can append to a file (new lines for example) like this:

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

and there are so many other ways. Leverage the System.Io namespace.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232