加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 19:44:41 所属栏目:百科 来源:网络整理
导读:我必须打开并从.txt文件中读取,这是我正在使用的代码: Stream myStream;openFileDialog1.FileName = string.Empty; openFileDialog1.InitialDirectory = "F:";if (openFileDialog1.ShowDialog() == DialogResult.OK) { var compareType = StringCompariso
我必须打开并从.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");
    } 
}

上面的代码抛出一个异常,说“空路径名不合法”.

我究竟做错了什么?

解决方法

正如 hmemcpy所指出的,您的问题如下

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:usersomeFile.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,"Directory");
}

你应该做的是将代码改为类似的东西

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读