3

I have to open and read from a .txt file, here is the code I'm using:

Stream myStream;
openFileDialog1.FileName = string.Empty; 
openFileDialog1.InitialDirectory = "F:\\";
if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{
    var compareType = StringComparison.InvariantCultureIgnoreCase;
    var fileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
    var extension = Path.GetExtension(openFileDialog1.FileName);
    if (extension.Equals(".txt", compareType))
    {
        try 
        { 
            using (myStream = openFileDialog1.OpenFile()) 
            { 
                string file = Path.GetFileName(openFileDialog1.FileName);
                string path = Path.GetFullPath(file); //when i did it like this it's work fine but all the time give me same path whatever where my "*.txt" file is
                //Insert code to read the stream here. 
                //fileName = openFileDialog1.FileName; 
                StreamReader reader = new StreamReader(path);
                MessageBox.Show(file, "fileName");
                MessageBox.Show(path, "Directory");
            } 
        } 
        // Exception thrown: Empty path name is not legal
        catch (ArgumentException ex) 
        { 
            MessageBox.Show("Error: Could not read file from disk. " +
                            "Original error: " + ex.Message); 
        } 
    }
    else 
    {
        MessageBox.Show("Invaild File Type Selected");
    } 
} 

The code above throws an exception which says "Empty path name is not legal".

What am I doing wrong?

4
  • how to read 2 matrix from txt file using openFileDialog Commented Jun 12, 2011 at 12:45
  • It depends on how they are stored there. Can you provide more details? Commented Jun 12, 2011 at 12:46
  • Harry - in your code you have a comment saying "//i've got exception for now:(" is this what the problem is? Can you copy and paste the exception. People are trying to help here but you're not helping us. Please be more specific as to what the problem is. Thanks. Commented Jun 12, 2011 at 12:54
  • Kev it's what exception says "Empty path name is not legal" Commented Jun 12, 2011 at 13:00

4 Answers 4

7

As pointed by hmemcpy, your problem is in the following lines

using (myStream = openFileDialog1.OpenFile())
{
   string file = Path.GetFileName(openFileDialog1.FileName);
   string path = Path.GetDirectoryName(file);
   StreamReader reader = new StreamReader(path);
   MessageBox.Show(file, "fileName");
   MessageBox.Show(path, "Directory");
} 

I'm going to break down for you:

/*
 * Opend the file selected by the user (for instance, 'C:\user\someFile.txt'), 
 * creating a FileStream
 */
using (myStream = openFileDialog1.OpenFile())
{
   /*
    * Gets the name of the the selected by the user: 'someFile.txt'
    */
   string file = Path.GetFileName(openFileDialog1.FileName);

   /*
    * Gets the path of the above file: ''
    *
    * That's because the above line gets the name of the file without any path.
    * If there is no path, there is nothing for the line below to return
    */
   string path = Path.GetDirectoryName(file);

   /*
    * Try to open a reader for the above bar: Exception!
    */
   StreamReader reader = new StreamReader(path);

   MessageBox.Show(file, "fileName");
   MessageBox.Show(path, "Directory");
} 

What you should do is to cahnge the code to something like

using (myStream = openFileDialog1.OpenFile())
{
   // ...
   var reader = new StreamReader(myStream);
   // ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

i changed that what u write and now i got no path only white space in 2nd Messagebox. Any idea why?
change the line string path = Path.GetDirectoryName(file) to string path = Path.GetDirectoryName(openFileDialog1.FileName)
6

You want user to select only .txt files? Then use the .Filter property, like that:

openFileDialog1.Filter = "txt files (*.txt)|*.txt";

2 Comments

i did it in properties of openFiledialog control
Then that's your problem? How to create an instance of the class?
5

Your bug is in the lines:

string file = Path.GetFileName(openFileDialog1.FileName);
string path = Path.GetDirectoryName(file);

In the first line the file variable will only contain the file name, e.g. MyFile.txt, making the second line return an empty string to the path variable. Further down your code you'll attempt to create a StreamReader with an empty path, and this what throws the exception.

By the way, this is exactly what the exception tells you. If you remove the try..catch around the using block, you would've seen it happen during debug in your Visual Studio.

2 Comments

oh ok :) then how get all path of this file?
You already have the path of the file in openFileDialog1.FileName
2

StreamReader accepts Stream type of object while you are passing it a string. try this,

Stream myStream;

        using (myStream = openFileDialog1.OpenFile())
        {
            string file = Path.GetFileName(openFileDialog1.FileName);
            string file2 = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);

            string path = Path.GetDirectoryName(openFileDialog1.FileName);

            StreamReader reader = new StreamReader(myStream);

            while (!reader.EndOfStream)
            {
                MessageBox.Show(reader.ReadLine());
            }

            MessageBox.Show(openFileDialog1.FileName.ToString());
            MessageBox.Show(file, "fileName");
            MessageBox.Show(path, "Directory");
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.