2011-06-12 67 views
3

我都开并从.txt文件读取,这里是我使用的代码:如何在c#中使用openFileDialog打开文件.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"); 
    } 
} 

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

我在做什么错?

+0

如何读取使用的OpenFileDialog – harry180 2011-06-12 12:45:19

+0

txt文件2矩阵这取决于他们如何被存储在那里。你能提供更多细节吗? – VMAtm 2011-06-12 12:46:43

+0

哈利 - 在你的代码中,你有一个评论说:“//我现在有异常:(''这是什么问题?你能复制和粘贴异常吗?人们正试图帮助这里,请帮助我们解决问题,谢谢 – Kev 2011-06-12 12:54:36

回答

7

正如指出的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, "fileName"); 
    MessageBox.Show(path, "Directory"); 
} 

你应该做的是将代码cahnge到像

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

我改变了你写的,现在我没有路径只有第二个消息框中的空白。任何想法为什么? – harry180 2011-06-12 13:28:27

+0

更改行'字符串路径= Path.GetDirectoryName(文件)'为'字符串路径= Path.GetDirectoryName(openFileDialog1.FileName)' – 2011-06-12 13:29:55

+0

确定它现在工作正常:)谢谢 – harry180 2011-06-12 13:32:58

5

您希望用户只选择.txt文件吗? 然后使用.Filter属性,像:

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

我做了openFiledialog控件的属性 – harry180 2011-06-12 12:47:10

+0

那么这就是你的问题?如何创建这个类的一个实例? – VMAtm 2011-06-12 12:47:52

+0

问题写在了代码示例 – harry180 2011-06-12 13:04:06

5

你的错误是在线路:

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

在第一行的file变量将只包含文件名,例如MyFile.txt,使第二行返回一个空字符串到path变量。再往下看你的代码,你将尝试创建一个空路径的StreamReader,这引发了异常。

顺便说一下,这正是异常告诉你的。如果您在使用块周围删除try..catch,则在Visual Studio的调试过程中您会看到它发生。

+0

的注释中哦,好吧:)那么怎么得到这个文件的所有路径呢? – harry180 2011-06-12 13:11:11

+0

你已经有'openFileDialog1.FileName'文件的路径 – 2011-06-12 13:12:25

1

StreamReader接受Stream类型的对象whi你传递给它一个字符串。 试试这个,

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"); 
     } 
相关问题