C# 如何在c中使用openFileDialog打开file.txt文件?

C# 如何在c中使用openFileDialog打开file.txt文件?,c#,.net,winforms,C#,.net,Winforms,我必须打开并读取.txt文件,下面是我使用的代码: Stream myStream; openFileDialog1.FileName = string.Empty; openFileDialog1.InitialDirectory = "F:\\"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { var compareType = StringComparison.InvariantCultureIgnoreCase;

我必须打开并读取.txt文件,下面是我使用的代码:

Stream myStream;
openFileDialog1.FileName = string.Empty; 
openFileDialog1.InitialDirectory = "F:\\";
if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{
    var compareType = StringComparison.InvariantCultureIgnoreCase;
    var fileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
    var extension = Path.GetExtension(openFileDialog1.FileName);
    if (extension.Equals(".txt", compareType))
    {
        try 
        { 
            using (myStream = openFileDialog1.OpenFile()) 
            { 
                string file = Path.GetFileName(openFileDialog1.FileName);
                string path = Path.GetFullPath(file); //when i did it like this it's work fine but all the time give me same path whatever where my "*.txt" file is
                //Insert code to read the stream here. 
                //fileName = openFileDialog1.FileName; 
                StreamReader reader = new StreamReader(path);
                MessageBox.Show(file, "fileName");
                MessageBox.Show(path, "Directory");
            } 
        } 
        // Exception thrown: Empty path name is not legal
        catch (ArgumentException ex) 
        { 
            MessageBox.Show("Error: Could not read file from disk. " +
                            "Original error: " + ex.Message); 
        } 
    }
    else 
    {
        MessageBox.Show("Invaild File Type Selected");
    } 
} 
上面的代码抛出一个异常,该异常表示空路径名不合法


我做错了什么?

您希望用户只选择.txt文件吗? 然后使用该属性,如下所示:

openFileDialog1.Filter = "txt files (*.txt)|*.txt";

您的错误如下所示:

string file = Path.GetFileName(openFileDialog1.FileName);
string path = Path.GetDirectoryName(file);
在第一行中,file变量将只包含文件名,例如MyFile.txt,使得第二行向path变量返回一个空字符串。在代码的后面,您将尝试创建一个具有空路径的StreamReader,这就是引发异常的原因

顺便说一下,这正是异常告诉您的。如果删除了using块的try..catch,您将看到它在Visual Studio中的调试过程中发生。

正如前面所指出的,您的问题如下所示

using (myStream = openFileDialog1.OpenFile())
{
   string file = Path.GetFileName(openFileDialog1.FileName);
   string path = Path.GetDirectoryName(file);
   StreamReader reader = new StreamReader(path);
   MessageBox.Show(file, "fileName");
   MessageBox.Show(path, "Directory");
} 
我要为你说:

/*
 * Opend the file selected by the user (for instance, 'C:\user\someFile.txt'), 
 * creating a FileStream
 */
using (myStream = openFileDialog1.OpenFile())
{
   /*
    * Gets the name of the the selected by the user: 'someFile.txt'
    */
   string file = Path.GetFileName(openFileDialog1.FileName);

   /*
    * Gets the path of the above file: ''
    *
    * That's because the above line gets the name of the file without any path.
    * If there is no path, there is nothing for the line below to return
    */
   string path = Path.GetDirectoryName(file);

   /*
    * Try to open a reader for the above bar: Exception!
    */
   StreamReader reader = new StreamReader(path);

   MessageBox.Show(file, "fileName");
   MessageBox.Show(path, "Directory");
} 
你应该做的是把代码压缩成

using (myStream = openFileDialog1.OpenFile())
{
   // ...
   var reader = new StreamReader(myStream);
   // ...
}

StreamReader在传递字符串时接受对象的流类型。 试试这个

Stream myStream;

        using (myStream = openFileDialog1.OpenFile())
        {
            string file = Path.GetFileName(openFileDialog1.FileName);
            string file2 = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);

            string path = Path.GetDirectoryName(openFileDialog1.FileName);

            StreamReader reader = new StreamReader(myStream);

            while (!reader.EndOfStream)
            {
                MessageBox.Show(reader.ReadLine());
            }

            MessageBox.Show(openFileDialog1.FileName.ToString());
            MessageBox.Show(file, "fileName");
            MessageBox.Show(path, "Directory");
        }

如何使用OpenFileDialog从txt文件中读取2矩阵取决于它们在其中的存储方式。你能提供更多的细节吗?Harry-在你的代码中有一条评论说//我现在有个例外:这就是问题所在吗?你能复制并粘贴例外情况吗。这里的人都想帮忙,但你帮不了我们。请更具体地说明问题所在。谢谢,Kev这是什么例外说空路径名是不合法的我在openFiledialog控件的属性中做的那是你的问题?如何创建该类的实例?哦,好的:那么如何获取该文件的所有路径?您已经在openFileDialog1.FileName中拥有该文件的路径。我更改了您编写的内容,现在在第二个Messagebox中没有路径,只有空白。知道为什么吗?将字符串path=path.GetDirectoryNamefile更改为字符串path=path.GetDirectoryNameopenFileDialog1.FileName