How to get the full path of a directory by using it's name?

I want to check for a specific file in Downloads directory and delete it if it exists?
Someone please tell me how to get the full path of “Downloads” directory? Because If I use the full path, it doesn’t work when others use the same code, they need to change the path again.

Thanks and Regards,
Namratha

Hi,

The full path can be something like below.
C:\Users\user_name\Downloads

The user name can be taken as a variable, if different user log in, his username value can be given in the full path.

Thanks,
Saranya K R

2 Likes

Hi,
You can find all special folder using the below code, it gives the user desktop path.
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

But Download Path is not treated as special folder so you can try below code
Environment.ExpandEnvironmentVariables(“%USERPROFILE%\Downloads”)

Thanks

3 Likes

@Bharat, Environment.ExpandEnvironmentVariables(“%USERPROFILE%\Downloads”) this one worked.

Thanks a ton :slight_smile:

Thanks a lot for the solution @SaranyaKishore. Currently used the suggestion provided by @Bharat. But definitely your solution will be helpful in future :slight_smile:

But I am getting one more problem in this. Suppose if store the path in a variable say "downloadDirPath. then using directory.GetFiles(downloadDirPath) doesn’t give the list of files. Instead it prints the System.string. Actually now I want to list out all the files and search the specific file out of those.

directory.GetFiles(downloadDirPath) will store the filenames in an array.
Use a for each loop to access all the files.

Untitled

1 Like

@SaranyaKishore

Thanks a ton, it worked.