vb.net 如何发现用户的桌面文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6463280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I discover the user's Desktop folder?
提问by Mc SH1FTY
I'm making a little application in visual studio which loads a ROM in an emulator. I have two emulators and 20 ROMs.
我正在 Visual Studio 中制作一个小应用程序,它在模拟器中加载一个 ROM。我有两个模拟器和 20 个 ROM。
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:
我制作了一个表格并添加了几个按钮。当您单击按钮时,它会打开一个新表单并关闭旧表单。然后在新表单上我有四个按钮:每个按钮在模拟器中加载不同的 ROM。因此,当您按下 Button1 时,会触发此代码:
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.
它工作正常 - 我点击它并在模拟器中加载游戏。我遇到的问题是文件路径。如果我将此应用程序发送给朋友,它仍会查找“C:\Users\shifty\Desktop\” - 但那是在我的计算机上,而不是他的计算机上。
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))
有没有办法让应用程序在他的计算机上查找文件(无需将文件路径更改为 (C:\Users\""his user name""\Desktop))
回答by Spence
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
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.
它甚至可以在 XP、vista 和 Windows 7 之间正常工作。
回答by thedsz
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.
旧帖子,但我必须支持 Mc Shifty。你不能假设每个人都是编码专家。如果是的话,他们就不会在这里问这样的问题了。
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.
Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
<<< 包括和额外 )
Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
<<< 额外 ) 和 ; 是 C 或 java 而不是 VB,他显然在示例代码中使用了它。
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
上面的代码会给你需要的结果,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
回答by Mark Carpenter
There's a mechanism to get the current user's Desktop directory, using Environment.SpecialFolder
.
有一种机制可以使用Environment.SpecialFolder
.
Usage:
用法:
Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
回答by Kyle
I had problems using the Environment.GetFolderPath
method from previous answers.
我在使用Environment.GetFolderPath
之前答案中的方法时遇到了问题。
The following works in VB 2012, My.Computer.FileSystem.SpecialDirectories.Desktop
以下作品在 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,
因此,如果您在用户桌面上有一个名为“contacts.txt”的文件,以下将显示完整路径,
' Desktop path
Dim desktopPath = My.Computer.FileSystem.SpecialDirectories.Desktop
' Concatenate desktop path and file name
filePath = desktopPath & "/contacts.txt"
MsgBox(filePath)
回答by HedonicKnight
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:
MC SH1FTY,我假设您已经想通了,但是要做您想做的事情:
1) Call in that code that Spence wrote as a variable (I'd declare it Globally, but that's my preference. To do that:
1) 调用 Spence 作为变量编写的代码(我会在全局范围内声明它,但这是我的偏好。要做到这一点:
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:
2)要么直接在你的代码中使用这个,要么制作另一个字符串来连接一个目录:
Option A)
选项 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
语句替换为:
Shell(emulatorPath & romPath, vbNormalFocus)
Or, Option B, which is thedsz's answer:
或者,选项 B,这是 dsz 的答案:
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
回答by Eran Brown
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")
- 把它放在表格的顶部
- “公开路径为字符串 = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)”
- 确保文件在他们的桌面上!
- 然后”单击您的按钮或您用来打开鸸鹋并键入的任何内容
- "Process.Start(thepath + "the emulator.exe "+ "the rom you want")
回答by Ray Kinsella
By using that you guarantee that the emulator is on the users desktop. This is not always the case. I know I move things around that I download or a friend sends to me. It's better to use App.Path and make sure your emulator.exe is in the directory with your little front end program (usually the case).
通过使用它,您可以保证模拟器在用户桌面上。这并非总是如此。我知道我会移动我下载或朋友发送给我的东西。最好使用 App.Path 并确保您的 emulator.exe 位于您的小前端程序所在的目录中(通常是这种情况)。
回答by John Kalberer
You need to use a file open dialog to choose your path for the two files. Hereis 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