I'm trying to search for all the *.bak files in the user's desktop. I'm using this to search for them.
string[] fNames = new DirectoryInfo(@"%homepath%\Desktop")
.GetFiles("*.bak")
.Select(x => x.Name)
.ToArray();
my path is @"%homepath%\Desktop"
(user's desktop)
While debugging, I'm getting an System.IO.DirectoryNotFoundException
, checking the path, I've noticed this:
As you can see, it's trying to append the given path (@"%homepath%\Desktop"
) to the path where the application is being ran from.
I've tried all sorts of different path formatting and always the same.
CodePudding user response:
On Windows, %HOMEPATH%
is not (always) where your user profile is located - e.g. if your Home Folder is redirected by AD then it will be that instead, and if the redirection is to a different drive, you will need %HOMEDRIVE%
.
If you want to locate the desktop folder and you are on Windows, you should use
var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
So your code should be:
string[] fNames = new DirectoryInfo(desktopPath)
.GetFiles("*.bak")
.Select(x => x.Name)
.ToArray();