[英]Why I'm getting error can't find file name in other folder then the one I gave?
private void GetMessages()
{
DirectoryInfo d = new DirectoryInfo(@"D:\Csharp Projects\Get Messages\Sources");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.html"); //Getting Text files
foreach (FileInfo file in Files)
{
using(StreamReader reader = new StreamReader(file.Name))
{
string line;
while((line = reader.ReadLine()) != null)
{
if(line.Contains("msg-bullet"))
{
int index = line.IndexOf("<");
int index1 = line.IndexOf(">");
string result = line.Substring(index, 4);
}
}
}
}
}
I'm getting the error on the line:我在线收到错误消息:
using(StreamReader reader = new StreamReader(file.Name))
Could not find file 'D:\Csharp Projects\Get Messages\bin\Debug\Source0.html
找不到文件 'D:\Csharp Projects\Get Messages\bin\Debug\Source0.html
The path from the error message D:\Csharp Projects\Get Messages\bin\Debug
isn't the path or even a subpath from the one I provided in the source, eg D:\Csharp Projects\Get Messages\Sources
错误消息
D:\Csharp Projects\Get Messages\bin\Debug
的路径不是我在源代码中提供的路径甚至是子路径,例如D:\Csharp Projects\Get Messages\Sources
Why isn't the given path used?为什么不使用给定的路径?
Solution is to use file.FullName:解决方案是使用 file.FullName:
using (StreamReader reader = new StreamReader(file.FullName))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.