Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I making a file browser which I want to load files into the code editor, a bit like the solution explorer in visual studio. I have this piece of code which opens a folder browser dialog and makes a file browser like this one. I want it so that if you double-click a file it will open that file in the editor. I am using a tree view and an image list.

C#
private void openFolderToolStripButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog obd = new FolderBrowserDialog();
            if (obd.ShowDialog() == DialogResult.OK)
            {
                path = obd.SelectedPath;
                Console.WriteLine("path is found");
                Console.WriteLine(path.ToString());
                if (path != "")
                {
                    flowLayoutPanel1.Width = 200;
                    DirectoryInfo directoryInfo = new DirectoryInfo(path);
                    DirectoryInfo[] directories = directoryInfo.GetDirectories();
                    foreach (FileInfo file in directoryInfo.GetFiles())
                    {
                        
                        if (file.Exists)
                        {
                            TreeNode nodes = treeView1.Nodes[0].Nodes.Add(file.Name);
                            SetImageExtension(file.Name, nodes);
                        }
                    }

                    if (directories.Length > 0)
                    {
                        foreach (DirectoryInfo directory in directories)
                        {
                            TreeNode node = treeView1.Nodes[0].Nodes.Add(directory.Name);
                            node.ImageIndex = node.SelectedImageIndex = 0;
                            foreach (FileInfo file in directory.GetFiles())
                            {
                                if (file.Exists)
                                {
                                    TreeNode nodes = treeView1.Nodes[0].Nodes[node.Index].Nodes.Add(file.Name);
                                    SetImageExtension(file.Name, nodes);
                                }
                            }
                        }
                    }
                }
            }
        }


What I have tried:

Well, I have this for and not sure how to actually read from the file.
C#
private void TreeView_NodeDoubleClick(object sender, MouseEventArgs e)
        {
            string nodeText = treeView1.SelectedNode.Text;
            string nPath = path + @"\" + nodeText;
            using (var fileStream = new FileStream(nPath, FileMode.Open, FileAccess.Read))
            {
                Console.WriteLine(nPath);
            }
        }
Posted
Updated 21-Feb-17 12:33pm
Comments
Patrice T 21-Feb-17 18:14pm    
What your question or problem ?
Graeme_Grant 21-Feb-17 18:25pm    
I've got it handled.
Rohit Pai (rodude123) 21-Feb-17 18:24pm    
Question is how do I open any file in c#

Google Search is your friend: c# read from a file[^]
 
Share this answer
 
Comments
Rohit Pai (rodude123) 21-Feb-17 18:22pm    
I don't want it to be a text file I want to it to be .html, .css, .js or .php files. Would It be the same process
Graeme_Grant 21-Feb-17 18:23pm    
they are text files, not binary.
Dave Kreskowiak 21-Feb-17 18:27pm    
ALL of which are nothing but text files.
Graeme_Grant 21-Feb-17 18:31pm    
yes, they are... ;)
Rohit Pai (rodude123) 21-Feb-17 18:34pm    
Yea it worked thanks
Quote:
Question is how do I open any file in c#

and google gave you no answer, no example, no nothing ?
Quote:
I don't want it to be a text file I want to it to be .html, .css, .js or .php files. Would It be the same process

All those files are text files, the file extension doesn't matter.

remember: Google is your friend.
At least do basic research before asking here, we are not a free search service.
 
Share this answer
 
Comments
Rohit Pai (rodude123) 21-Feb-17 18:36pm    
Sorry, I will do next time, I just didn't know an html/css file was classed as a text file. I googled how to open any file in c# and nothing really came
Patrice T 21-Feb-17 18:46pm    
Googling "open file c#" only gives 1,600,000 answers
Bryian Tan 21-Feb-17 19:42pm    
Here you get TWO answers. and both the right answers from the experts :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900