none
How to read lines from file into array in .NET CF C# ?

    Question

  • I'm new to C# and just installed Visual Studio 2008 Pro for use with C# and I'm creating a Windows Form for Mobile 6.1. I have Windows Mobile 6 Professional SDK installed, and Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1.

    I want to write a piece of code that reads the lines of a textfile into an array. In .NET C# this would be:  string[] lines = File.ReadAllLines("vragenlijst.txt"); but then with the ReadAllLines method not being available in CF I don't know how to do this? Code example?


    R.
    Monday, February 15, 2010 7:41 PM

Answers

  • Hi,

    The following code should give you the directory where the EXE is located :-

       string path;
       path = System.IO.Path.GetDirectoryName( 
          System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
       MessageBox.Show( path );
    

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Marked as answer by ruud00000 Tuesday, February 16, 2010 4:51 PM
    Monday, February 15, 2010 9:55 PM

All replies

  • Hi,

    The following code should help you :-

                List<string> fileContent = new List<string>();
    
                TextReader tr = new StreamReader(@"\Temp\CurrentFile.txt");
    
                string currentLine = string.Empty;
    
                while ((currentLine = tr.ReadLine()) != null)
                {
                    fileContent.Add(currentLine);
                }
                
                tr.Close();
    
                string[] lines = fileContent.ToArray();

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Marked as answer by ruud00000 Monday, February 15, 2010 8:17 PM
    • Unmarked as answer by ruud00000 Monday, February 15, 2010 8:55 PM
    Monday, February 15, 2010 7:54 PM
  • I tried this:

                using (StreamReader reader = File.OpenText("vragenlijst.txt"))
                {
                    while (!reader.EndOfStream)
                    {
                        vragenlijst.Add(reader.ReadLine());
                        aantalVragen++;
                    }
                }
    

    However the file is not found. Looking in the debugger info it says "Could not find file '\\vragenlijst.txt'." so adding two backslashes to the name I provided. Also when I include the full path, to avoid any confusion about the correct path, like File.OpenText("C:\\test\\vragenlijst.txt") it adds two backslashes before the path and gives the same error. 

    I also tried with the @-sign the way you provided but that doesn't help me either?

    Why is that? How to solve?



    R.
    Monday, February 15, 2010 9:05 PM
  • Hi,

    Where is the vragenlijst.txt file on the device? The filename you are using is looking for the file in the root of the device.

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Monday, February 15, 2010 9:10 PM
  • I was actually running it in the device emulator. Can a file not be found on the C: drive of the computer for that reason?

    When I run it on the actual device and place the textfile in the root it works.

    How exactly would I e.g. refer to the file in the directory where the program itself is located?
    R.
    Monday, February 15, 2010 9:50 PM
  • Hi,

    The following code should give you the directory where the EXE is located :-

       string path;
       path = System.IO.Path.GetDirectoryName( 
          System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
       MessageBox.Show( path );
    

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Marked as answer by ruud00000 Tuesday, February 16, 2010 4:51 PM
    Monday, February 15, 2010 9:55 PM