954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Weird, Stupid Problems in C#

Here's my error.
"System.NotSupportedException"

I have some problems with the StreamReader class.

I have a textbox, and with the textbox, the text is the files location. I have tried with a pattern('@'), and with using the double slash character, and NOTHING has worked.

Here is my function

static string ReadFile(string filePath)
        {
            string ret = "";
            string line;
            StreamReader file = new StreamReader(filePath);
            while ((line = file.ReadLine()) != null)
                ret += line;
            return ret;
        }


Here's usage

textBox1.Text = ReadFile(treeView2.SelectedNode.ToString());


And it is not the treeview messing up.


If I get the text from the TreeView, and put it there, I don't get errored, lmao, so stupid.

Thanks

glut
Light Poster
42 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Here's the documentation of TreeView.SelectedNode .

Your ReadFile function works just fine, so the problem has to be in the passed argument (filePath). You could add following lines to the beginning of ReadFile:

if (filePath == null) MessageBox.Show("FilePth has null value");
if (!File.Exists(filePath)) MessageBox.Show("File: '" + filePath + "' not found");

Your "System.NotSupportedException" message could be from a null file name (= no selected node is selected). Also, is treeView2.SelectedNode.ToString() correct or do you try to get treeView2.SelectedNode.FullPath ? In the latter case your TreeView2.PathSeparator should be @"\". Just a few thoughts...

HTH

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

To get the path, you have to use treeView2.SelectedNode.FullPath; property.
And its a bit strange to put all the files one beside another into textBox. At lease seperate them, by using some delimiter like commma:

//selected node:
textBox1.Text = ReadFile(treeView2.SelectedNode.FullPath;

//your method
        static string ReadFile(string filePath)
        {
            string ret = "";
            string line;
            StreamReader file = new StreamReader(filePath);
            while ((line = file.ReadLine()) != null)
                ret += line + ", "; //changed to seperate files
            return ret;
        }
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

Mitja Bonca,

Thank you so much, it now works! I was so pissed off, but thank you so much.

glut
Light Poster
42 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

:)
You are welcome.
bte, just take it easy... programming is about calmness, concentration and creativness.

bye

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: