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

Recommended Answers

All 4 Replies

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

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,

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

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

bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.