0

I am creating a button that will open one directory in your computer (for example: C:\Users\NameOfUser\Downloads) when you click it. But if I do this on another computer or from another user account with a different name the button doesn’t work. I just used Process.Start() for the button.

Is there a different way to do this that will work from any user account?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Lorenzo
  • 19
  • 6
  • Take a look at these two questions and see if that helps you out. They are C# examples but it's still .NET so you should be fine. https://stackoverflow.com/questions/10667012/getting-downloads-folder-in-c#21953690 https://stackoverflow.com/questions/3795023/how-to-programmatically-derive-windows-downloads-folder-userprofile-downloads – jaromey May 03 '18 at 20:56
  • Should be "NameOfUser" rather than "NameOfPC". It sounds like you named the user account on the machine the same as the computer, but you should understand this folder path is for a user, and not a computer. – Joel Coehoorn May 03 '18 at 21:02
  • 1
    A different user has a different downloads path (and let's not consider the localization question) You need to build the path looking at the Environment.UserName – Steve May 03 '18 at 21:10
  • 1
    @Steve It's worse than that. Windows lets you put the downloads folder anywhere you want. Lots of users like to move this to a D: drive, for example. So the path might not be related to the username at all. To really know the right answer, you need to check the registry (which is itself tricky, because how Windows uses the registry for this has changed over the years) or call a method that checks the registry in the right way for you. – Joel Coehoorn May 03 '18 at 21:18
  • Can you do an example of Enviroment.GetFolderPath? – Lorenzo May 03 '18 at 21:18
  • 1
    @JoelCoehoorn I know well the problem. Just look at the deleted answer in that question you have linked below. – Steve May 03 '18 at 21:21
  • 1
    @Steve Gotta love Stack Overflow ;) – Joel Coehoorn May 03 '18 at 21:24

1 Answers1

1

You can get most folder paths like this just by calling Environment.GetFolderPath() with the right Environment.SpecialFolder enum value. Unfortunately, the Downloads special folder in .Net isn't quite "special" enough and (imo) is unreasonably complicated to get. If you actually need to know the path, the correct way is to follow the accepted answer here:

Getting Downloads Folder in C#?

Even worse, it's written for C# and uses code that's not very easy to translate.

The good news is there's also a NuGet package I'd expect you can use from VB. The even better news is you don't really care about the exact path in this case. You just want to open an Explorer window via Process.Start(). That means you can use this shortcut (also available via the other question):

Process.Start("shell:Downloads")
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794