我必须打开并阅读.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 :(得分: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");
}
你应该做的是将代码改为
using (myStream = openFileDialog1.OpenFile())
{
// ...
var reader = new StreamReader(myStream);
// ...
}
答案 1 :(得分:6)
您希望用户只选择.txt文件吗? 然后使用.Filter属性,如下所示:
openFileDialog1.Filter = "txt files (*.txt)|*.txt";
答案 2 :(得分:5)
你的错误在于:
string file = Path.GetFileName(openFileDialog1.FileName);
string path = Path.GetDirectoryName(file);
在第一行中,file
变量仅包含文件名,例如MyFile.txt
,使第二行返回一个空字符串到path
变量。在代码的下方,您将尝试使用空路径创建StreamReader
,这就是抛出异常的原因。
顺便说一句,这正是异常告诉你的。如果删除使用块周围的try..catch
,您将在Visual Studio中调试期间看到它。
答案 3 :(得分:1)
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");
}