[英]How to open file .txt with using openFileDialog in c#?
我必須打開並讀取.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");
}
}
上面的代碼拋出一個異常,上面寫着"Empty path name is not legal" 。
我究竟做錯了什么?
正如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);
// ...
}
您希望用戶訪問 select only.txt 文件嗎? 然后使用.Filter屬性,如下所示:
openFileDialog1.Filter = "txt files (*.txt)|*.txt";
您的錯誤在以下行中:
string file = Path.GetFileName(openFileDialog1.FileName);
string path = Path.GetDirectoryName(file);
在第一行file
變量將只包含文件名,例如MyFile.txt
,使第二行返回一個空字符串給path
變量。 在您的代碼下方,您將嘗試創建一個帶有空路徑的StreamReader
,這就是引發異常的原因。
順便說一句,這正是異常告訴你的。 如果您刪除 using 塊周圍的try..catch
,您會在 Visual Studio 中的調試期間看到它發生。
StreamReader 在您傳遞字符串時接受 Stream 類型的 object。 嘗試這個,
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.