c# - 如何在 C# 中列出 %TEMP% 和 %USERNAME% 目录?

标签 c# list variables path directory

我试图在文本框中列出目录 %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/

相关文章:

c# - Powershell哈希表作为C#中自定义cmdlet的参数

c# - 使用变量的值设置面板的可见性

C# LINQ,删除所有相等的元素

arrays - Perl:在数组中搜索项目

C++ 变量在 While 循环中不更新

C# - 检测人脸和裁剪图像

python - 在 python 中创建新的列表对象

python - Python 上的列表索引错误

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

C# GroupBy - 创建多个分组级别