[英]C# ReadAllText File Doesn't Exist
我正在創建一個非常基本的程序來嘗試一些文件選項,在這種情況下,我想計算在 ReadAllText 的幫助下創建的文本文件中有多少個單詞,但是,路徑名總是顯示異常。
這是我的代碼:
var path = @"C:\Users\pandrews\countme.docx";
//Checks if file exists or not
if (File.Exists(path))
{
Console.WriteLine("File Exists");
var content = File.ReadAllText(path);
Console.WriteLine(content);
} else {
Console.WriteLine("File doesn't exist");
}
//Checks if directory exists, if it does it returns a list of files in the directory and shows on the console.
if(Directory.Exists(@"C:\Users\pandrews"))
{
Console.WriteLine("Directory exists");
var files = Directory.GetFiles(@"C:\Users\pandrews");
foreach(var item in files)
{
Console.WriteLine(item);
}
} else
{
Console.WriteLine("Directory doesn't Exist");
}
如您所見,我檢查文件是否存在,但visual studio 說它不存在,但是,如果我檢查目錄是否存在,它說它存在,如果我檢查文件列表,它會列出我想閱讀的文件.
C:\Users\pandrews\.gitconfig
C:\Users\pandrews\countme.docx
C:\Users\pandrews\NTUSER.DAT
C:\Users\pandrews\ntuser.dat.LOG1
C:\Users\pandrews\ntuser.dat.LOG2
C:\Users\pandrews\ntuser.ini
如果我使用 try catch,則在嘗試讀取所有文本時會顯示以下異常:
El nombre de archivo, el nombre de directorio o la sintaxis de la etiqueta del volumen no son correctos. : 'C:\Users\pandrews\source\repos\testingreadalltext\testingreadalltext\bin\Debug\netcoreapp3.1\??C:\Users\pandrews\countme.docx'
翻譯:文件名、目錄名或卷標語法不正確。
我在我的代碼中看不到任何錯誤,但希望其他人能夠看到。
編輯:我想添加我以管理員身份登錄,用戶管理員是文件的所有者,我以管理員身份執行了 Visual Studio。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.