Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I discover the user's Desktop folder?

I'm making a little application in visual studio which loads a ROM in an emulator. I have two emulators and 20 ROMs.

I made a form and added a few buttons. When you click the Button it opens a new form and closes the old one. Then on the new form I have four buttons: each one loads a different ROM in an emulator. So when you press Button1 this code is triggered:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
Shell("C:\Users\shifty\Desktop\pokemon games\Emulator\VBA\VisualBoyAdvance.exe ""C:\Users\shifty\Desktop\pokemon games\Roms\Yellow\Pokemon Yellow.gb""", vbNormalFocus)
End Sub

It works fine - I click it and it loads the game in the emulator. The bit im having trouble with is the file paths. If I send this application to a friend, it would still look for "C:\Users\shifty\Desktop\" - but that's on my computer, not his.

Is there a way to make the application look for the file on his computer (without changing the file path to (C:\Users\""his user name""\Desktop))

like image 598
Mc SH1FTY Avatar asked Jun 24 '11 04:06

Mc SH1FTY


5 Answers

Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

This will resolve to be the desktop folder for the current user.

It will even work between XP, vista and Windows 7 properly.

like image 190
Spence Avatar answered Nov 16 '22 13:11

Spence


Old post but I have to side with Mc Shifty. You can't assume that everyone is a coding expert. If they were then they wouldn't be here asking questions like that.

None of the answers given above were complete

Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) <<< includes and extra ) Environment.GetFolderPath(Environment.SpecialFolder.Desktop)); <<< extra ) and the ; is C or java not VB which he is obviously using by his example code.

Both of those only give you half of the required code to generate something usable.

Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

The above code will give you the result needed, c:\users\shifty\desktop

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
    Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    Shell(s & "\Desktop\pokemon games\Emulator\VBA\VisualBoyAdvance.exe " & s & "\pokemon games\Roms\Yellow\Pokemon Yellow.gb""", vbNormalFocus)
End Sub
like image 34
thedsz Avatar answered Nov 16 '22 14:11

thedsz


There's a mechanism to get the current user's Desktop directory, using Environment.SpecialFolder.

Usage:

Environment.GetFolderPath(Environment.SpecialFolder.Desktop));

like image 44
Mark Carpenter Avatar answered Nov 16 '22 14:11

Mark Carpenter


I had problems using the Environment.GetFolderPath method from previous answers.

The following works in VB 2012, My.Computer.FileSystem.SpecialDirectories.Desktop

So, if you have a file on a users desktop named "contacts.txt", the following will display the full path,

' Desktop path
Dim desktopPath = My.Computer.FileSystem.SpecialDirectories.Desktop

' Concatenate desktop path and file name
filePath = desktopPath & "/contacts.txt"

MsgBox(filePath)

Documentation

like image 29
Kyle Avatar answered Nov 16 '22 14:11

Kyle


Really old post at this point, but hey, found what I was looking for.

MC SH1FTY, I assume you have figured this out already, but to do what you are trying to do:

1) Call in that code that Spence wrote as a variable (I'd declare it Globally, but that's my preference. To do that:

Public userDesktopLoc As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

2) Either use this DIRECTLY in your code, or make another string to concatenate a directory:

Option A)

Public emulatorPath As String = userDesktopLoc & "pokemon games\Emulator\VBA\VisualBoyAdvance.exe "
Public romPath As String = userDesktopLoc & "pokemon games\Roms\Yellow\Pokemon Yellow.gb"

Then, within your Subroutine, replace your current Shell statement with:

Shell(emulatorPath & romPath, vbNormalFocus)

Or, Option B, which is thedsz's answer:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
        Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        Shell(s & "\Desktop\pokemon games\Emulator\VBA\VisualBoyAdvance.exe " & s & "\pokemon games\Roms\Yellow\Pokemon Yellow.gb""", vbNormalFocus) 
End Sub
like image 25
HedonicKnight Avatar answered Nov 16 '22 14:11

HedonicKnight