将StreamReader与GetFolderPath()一起使用
本文关键字:一起 StreamReader GetFolderPath | 更新日期: 2023-09-27 18:25:27
我有一行可以获取文本文件的文件路径:
string file = Path.Combine(Environment.GetFolderPath
(Environment.SpecialFolder.ApplicationData), "file.txt");
哪个返回:
file = "C:''Users''Benjamin''AppData''Roaming''file.txt"
但是,当我尝试在上面使用StreamReader时,它会返回一个FileNotFound
异常
StreamReader rdr = new StreamReader(file); // Throws the FileNotFound exception.
原始位置与返回的位置不同吗?(原始位置:C:'Users'Benjamin'Documents'file.txt
)
我猜C:'Users'Benjamin'AppData'Roaming'file.txt
不存在。
如果需要C:'Users'Benjamin'Documents'file.txt
,请使用Environment.SpecialFolder.MyDocuments
而不是Environment.SpecialFolder.ApplicationData
Path.Combine只是构建一个路径,它不会检查文件是否存在,也不会在文件不存在时执行任何操作。
正如异常所示,您只是试图打开一个不存在的文件。