How do I discover the user's Desktop folder?

2020-06-20 20:02发布

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))

8条回答
仙女界的扛把子
2楼-- · 2020-06-20 20:08

You need to use a file open dialog to choose your path for the two files. Here is an example.

You then use the two paths in your code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
    Shell(emulatorPath + "\"" + romPath + "\"", vbNormalFocus)
End Sub
查看更多
Bombasti
3楼-- · 2020-06-20 20:13

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.

查看更多
叼着烟拽天下
4楼-- · 2020-06-20 20:14

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
查看更多
一纸荒年 Trace。
5楼-- · 2020-06-20 20:15

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

Usage:

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

查看更多
叛逆
6楼-- · 2020-06-20 20:16

the answer is simple.

  • put this at the top of the form
  • "Public thepath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)"
  • that ensures that the file is on their desktop!
  • then" click on your button or whatever you used to open the emu and type
  • "Process.Start(thepath + "the emulator.exe "+ "the rom you want")
查看更多
孤傲高冷的网名
7楼-- · 2020-06-20 20:24

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
查看更多
登录 后发表回答