[英]How to open file .txt with using openFileDialog in c#?
I have to open and read from a .txt
file, here is the code I'm using:我必须打开并读取
.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");
}
}
The code above throws an exception which says "Empty path name is not legal" .上面的代码抛出一个异常,上面写着"Empty path name is not legal" 。
What am I doing wrong?我究竟做错了什么?
As pointed by hmemcpy , your problem is in the following lines正如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");
}
I'm going to break down for you:我为你分解:
/*
* 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");
}
What you should do is to cahnge the code to something like您应该做的是将代码更改为类似
using (myStream = openFileDialog1.OpenFile())
{
// ...
var reader = new StreamReader(myStream);
// ...
}
Your bug is in the lines:您的错误在以下行中:
string file = Path.GetFileName(openFileDialog1.FileName);
string path = Path.GetDirectoryName(file);
In the first line the file
variable will only contain the file name, eg MyFile.txt
, making the second line return an empty string to the path
variable.在第一行
file
变量将只包含文件名,例如MyFile.txt
,使第二行返回一个空字符串给path
变量。 Further down your code you'll attempt to create a StreamReader
with an empty path, and this what throws the exception.在您的代码下方,您将尝试创建一个带有空路径的
StreamReader
,这就是引发异常的原因。
By the way, this is exactly what the exception tells you.顺便说一句,这正是异常告诉你的。 If you remove the
try..catch
around the using block, you would've seen it happen during debug in your Visual Studio.如果您删除 using 块周围的
try..catch
,您会在 Visual Studio 中的调试期间看到它发生。
StreamReader accepts Stream type of object while you are passing it a string. StreamReader 在您传递字符串时接受 Stream 类型的 object。 try this,
尝试这个,
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.