我需要通过运行控制台应用程序删除文件夹中的某些文件。但是,它似乎不起作用,我不确定在这里做错了什么吗?
这是我正在使用的代码:
string[] filePaths = Directory.GetFiles(@"% USERPROFILE %\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\");
foreach (string filePath in filePaths)
File.Delete(filePath);
而当运行该应用程序时,这是我得到的错误:
Could not find a part of the path 'C:\Users\PC\source\repos\InstallUpdate\InstallUpdate\bin\Debug\netcoreapp3.1\% USERPROFILE %\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup'.'
看来它是使用netcoreapp3.1
的默认位置吗?
谢谢
尝试使用 Environment.ExpandEnvironmentVariables
string path = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\");
var filesToDelete = Directory.GetFiles(path);