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

基思

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

这是我正在使用的代码:

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);

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章