如何在C#中打开任何文件 [英] How do I open any file in C#
问题描述
我制作了一个文件浏览器,我想将文件加载到代码编辑器中,有点像Visual Studio中的解决方案浏览器。我有这段代码打开一个文件夹浏览器对话框,并使文件浏览器像这样一个。我想要它,这样如果你双击一个文件,它将在编辑器中打开该文件。我正在使用树状视图和图像列表。
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.
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.
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);
}
}
推荐答案
谷歌搜索是你的朋友: c#从文件中读取 [ ^ ]
Google Search is your friend: c# read from a file[^]
问题是如何在c#中打开任何文件
Question is how do I open any file in c#
而google没有给你答案,没有例子,没有什么?
and google gave you no answer, no example, no nothing ?
我不希望它是一个文本文件,我希望它是.html,.css,.js或.php文件。它是同一个过程
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.
这篇关于如何在C#中打开任何文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!