微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

| 我必须打开并从
.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:\\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,\"Directory\");
} 
您应该做的是将代码更改为类似
using (myStream = openFileDialog1.OpenFile())
{
   // ...
   var reader = new StreamReader(myStream);
   // ...
}
    ,        您要用户仅选择.txt文件吗? 然后使用.Filter属性,如下所示:
openFileDialog1.Filter = \"txt files (*.txt)|*.txt\";
    ,        您的错误所在:
string file = Path.GetFileName(openFileDialog1.FileName);
string path = Path.GetDirectoryName(file);
在第一行中,“ 7”变量将仅包含文件名,例如
MyFile.txt
,使第二行将空字符串返回到
path
变量。再往下看,您将尝试创建带有空路径的
StreamReader
,这将引发异常。 顺便说一句,这正是异常告诉您的内容。如果删除using块周围的ѭ11,您会发现它在Visual Studio调试期间发生。     ,        当您向对象传递字符串时,StreamReader接受对象的Stream类型。 尝试这个,
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\");
        }
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。