[英]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);
}
}
}
}
}
我在線收到錯誤消息:
using(StreamReader reader = new StreamReader(file.Name))
找不到文件 'D:\Csharp Projects\Get Messages\bin\Debug\Source0.html
錯誤消息D:\Csharp Projects\Get Messages\bin\Debug
的路徑不是我在源代碼中提供的路徑甚至是子路徑,例如D:\Csharp Projects\Get Messages\Sources
為什么不使用給定的路徑?
解決方案是使用 file.FullName:
using (StreamReader reader = new StreamReader(file.FullName))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.