[英]How to use openfiledialog to open any file as text in C#?
I am writing a winforms program in C# that uses the openfiledialog. 我正在C#中编写一个使用openfiledialog的winforms程序。 I would like it to be able to take the file that the user selected and open it as text, regardless of the file type. 我希望它能够将用户选择的文件作为文本打开,而不管文件类型如何。
I tried it like this: 我这样尝试过:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = Process.Start("notepad.exe", openFileDialog1.ToString()).ToString();
}
However, that didn't work and I'm not sure if I"m even on the right track. 但是,这没有用,我不确定我是否走在正确的轨道上。
You should use this code: 您应该使用以下代码:
First add this namespace : 首先添加以下名称空间:
using System.IO;
Then add this codes to your function: 然后将此代码添加到您的函数中:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (openFileDialog.ShowDialog()== DialogResult.OK)
{
textBox1.Text = File.ReadAllText(openFileDialog.FileName);
}
To open the file using notepad, you need to pass the file name as second parameter of Start
method. 要使用记事本打开文件,您需要将文件名作为Start
方法的第二个参数传递。 For example: 例如:
using (var ofd = new OpenFileDialog())
{
if(ofd.ShowDialog()== DialogResult.OK)
{
System.Diagnostics.Process.Start("notepad.exe", ofd.FileName);
}
}
Also if for any reason while knowing that not all file contents are text, you are going to read the file content yourself: 同样,如果由于某种原因知道并非所有文件内容都是文本,那么您将自己阅读文件内容:
using (var ofd = new OpenFileDialog())
{
if(ofd.ShowDialog()== DialogResult.OK)
{
var txt = System.IO.File.ReadAllText(ofd.FileName);
}
}
What you are doing at the moment is starting a Process
with the argument openFileDialog1.ToString()
, calling ToString()
on the process and setting this as text in the TextBox. 您目前正在执行的操作是使用参数openFileDialog1.ToString()
启动一个Process
,在该进程上调用ToString()
并将其设置为TextBox中的文本。 If the path was valid, the result would probably be something like "System.Diagnostics.Process". 如果路径有效,则结果可能类似于“ System.Diagnostics.Process”。 But since you use openFileDialog1.ToString()
as a path, your application probably crashes with a file not found error. 但是,由于您使用openFileDialog1.ToString()
作为路径,因此您的应用程序可能因未找到文件错误而崩溃。
To get the selected file of an OpenFileDialog
, use openFileDialog1.FileName
. 要获取OpenFileDialog
的选定文件,请使用openFileDialog1.FileName
。 (See the docs here ) (请参阅此处的文档)
What I think you actually want to do, is read from the file and write its contents as text to the TextBox. 我认为您真正想做的是从文件读取并将其内容作为文本写到TextBox。 To do this, you need a StreamReader
, like so: 为此,您需要一个StreamReader
,如下所示:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
using(var reader = new StreamReader(openFileDialog1.FileName))
{
textBox1.Text = reader.ReadToEnd();
}
}
This way, you open the file with the StreamReader, read its contents and then assign them to the text box. 这样,您可以使用StreamReader打开文件,读取其内容,然后将其分配给文本框。
The using
statement is there because a StreamReader
needs to be disposed of after you're done with it so that the file is no longer in use and all resources are released. 之所以using
语句,是因为StreamReader
用完后需要将其丢弃,以便不再使用该文件并释放所有资源。 The using statement does this for you automatically. using语句会自动为您执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.