Warm tip: This article is reproduced from serverfault.com, please click

c#-从文件夹中删除文件似乎不起作用?

(c# - Removing files from folder does not seem to be working?)

发布于 2020-12-02 07:16:59

我需要通过运行控制台应用程序删除文件夹中的某些文件。但是,它似乎不起作用,我不确定在这里做错了什么吗?

这是我正在使用的代码:

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的默认位置吗?

谢谢

Questioner
Keith
Viewed
0
puko 2020-12-02 15:26:52

尝试使用 Environment.ExpandEnvironmentVariables

    string path = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\");
    var filesToDelete = Directory.GetFiles(path);

https://docs.microsoft.com/zh-cn/dotnet/api/system.environment.expandenvironmentvariables?redirectedfrom=MSDN&view=net-5.0#System_Environment_ExpandEnvironmentVariables_System_String _