[英]Reading from a File in current User appdata folder for C#
I'm trying to read from a file inside the current user's appdata folder in C#, but I'm still learning so I have this: 我正在尝试从C#中当前用户的appdata文件夹中的文件读取,但是我仍在学习,因此我有以下内容:
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt");
while ((line = file.ReadLine()) != null)
{
Console.WriteLine(line);
counter++;
}
file.Close();
// Suspend the screen.
Console.ReadLine();
But I don't know what to type to make sure it's always the current user's folder. 但是我不知道要键入什么来确保它始终是当前用户的文件夹。
I might be misunderstanding your question but if you want to to get the current user appdata folder you could use this: 我可能会误解您的问题,但是如果要获取当前用户的appdata文件夹,可以使用以下命令:
string appDataFolder = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData);
so your code might become: 因此您的代码可能会变成:
string appDataFolder = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData
);
string filePath = Path.Combine(appDataFolder, "test.txt");
using (var reader = new StreamReader(filePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
or even shorter: 甚至更短:
string appDataFolder = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData
);
string filePath = Path.Combine(appDataFolder, "test.txt");
File.ReadAllLines(filePath).ToList().ForEach(Console.WriteLine);
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Take a look at the Environment.GetFolderPath method, and the Environment.SpecialFolder enumeration. 看一下Environment.GetFolderPath方法和Environment.SpecialFolder枚举。 To get the current user's app data folder, you can use either: 要获取当前用户的应用程序数据文件夹,可以使用以下任一方法:
Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData )
to get application directory for the current, roaming user. Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData )
获取当前漫游用户的应用程序目录。 This directory is stored on the server and it's loaded onto a local system when the user logs on, or 该目录存储在服务器上,并在用户登录时加载到本地系统上,或者 Environment.GetFolderPath( Environment.SpecialFolder.LocalApplicationData )
to get the application directory for the current, non-roaming user. Environment.GetFolderPath( Environment.SpecialFolder.LocalApplicationData )
获取当前非漫游用户的应用程序目录。 This directory is not shared between the computers on the network. 该目录不在网络上的计算机之间共享。 Also, use Path.Combine to combine your directory and the file name into a full path: 另外,使用Path.Combine将目录和文件名合并为完整路径:
var path = Path.Combine( directory, "test.txt" );
Consider using File.ReadLines to read the lines from the file. 考虑使用File.ReadLines从文件中读取行。 See Remarks on the MSDN page about the differences between File.ReadLines
and File.ReadAllLines
. 有关File.ReadLines
和File.ReadAllLines
之间的区别,请参见MSDN页面上的备注。
foreach( var line in File.ReadLines( path ) )
{
Console.WriteLine( line );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.