請找到更新的代碼。在這裏我試圖打開一個存在的文件。這是代碼片段。 sample.txt是獲取完整的文件路徑。我手動使用該路徑並能夠打開文本文件。但通過代碼我無法打開它。無法使用C#中的System.IO.File.OpenText(文件路徑)打開文件。
private void button1_Click(object sender, EventArgs e)
{
//to search for a file in folder
String name_of_file = textBox1.Text;
String search_file = @"D:\Shreyas\" + name_of_file + ".txt";
System.IO.File.WriteAllText("D:\\Shreyas\\sample.txt", search_file);
//search and open the file
if(System.IO.File.Exists(search_file))
{
MessageBox.Show("File Found!!");
System.IO.File.OpenText(search_file);
}
else
{
MessageBox.Show("File Doesn't Exist!!");
}
}
'd:\\\\ Shreyas \\\\ shreyas.txt '不是一個有效的路徑。 – Equalsk
試試'「D:\\ Shreyas \\ shreyas.txt」'而不是'「D:\\\\ Shreyas \\\\ shreyas.txt」' – Alexei
您是否能夠找到該文件並且不能打開它,或者你能不能找到它?將代碼片段轉儲到SO問題中並不意味着這是一個問題,請告訴我們您嘗試了什麼,以及您需要什麼幫助。 –