我都开并从.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");
}
}
上面的代码抛出一个异常,说:“空路径名称是不合法的”。
我在做什么错?
如何读取使用的OpenFileDialog – harry180 2011-06-12 12:45:19
txt文件2矩阵这取决于他们如何被存储在那里。你能提供更多细节吗? – VMAtm 2011-06-12 12:46:43
哈利 - 在你的代码中,你有一个评论说:“//我现在有异常:(''这是什么问题?你能复制和粘贴异常吗?人们正试图帮助这里,请帮助我们解决问题,谢谢 – Kev 2011-06-12 12:54:36