我试图在文本框中列出目录 %TEMP% 和 %USERNAME% 中的文件,以便人们可以看到那里存在哪些文件。当我键入 C:\users\%username%... 它无法工作。 这是我的代码:
string strDirLocal = @"C:\users\USERPROFILE\desktop";
if (System.IO.Directory.Exists(strDirLocal))
{
foreach (string sPath in System.IO.Directory.GetFiles(strDirLocal, "*.*"))
{
textBox1.Text = textBox1.Text + sPath.Replace(strDirLocal + @"\", "") + "\r\n";
感谢您花时间阅读本文。
最佳答案
您需要做的是在您的程序中扩展%TEMP%
和%USERPROFILE%
环境变量并使用结果而不是变量
string userProfile = Environment.GetEnvironmentVariable("USERPROFILE");
string strDirLocal = Path.Combine(userProfile, "desktop");
关于c# - 如何在 C# 中列出 %TEMP% 和 %USERNAME% 目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5493380/