I need to remove some files that are located in a folder by running the console application. However it does not seem to be working and I am not sure what I am doing wrong here?
This is the code I am using:
string[] filePaths = Directory.GetFiles(@"% USERPROFILE %\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\");
foreach (string filePath in filePaths)
File.Delete(filePath);
And when running the app this is the error I am getting:
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'.'
It seem it is using the netcoreapp3.1
as the default location?
Thanks
Try to use Environment.ExpandEnvironmentVariables
string path = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\");
var filesToDelete = Directory.GetFiles(path);