How do I discover the user's Desktop folder? - vb.net

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

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.

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

There's a mechanism to get the current user's Desktop directory, using Environment.SpecialFolder.
Usage:
Environment.GetFolderPath(Environment.SpecialFolder.Desktop));

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

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

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

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

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

Related

VB.NET programmatically do drag-and-drop from my local file system to a 3rd party application via my own user form

I am kind of lost trying to figure out the way how to drag and drop a file using a form.
Say I have my own files explorer app made in VB.NET and what I am looking for is that when I click on a Picturebox_Icon and drag it to another app (in my case SOLIDWORKS) that it drags and drops the file into SOLDIWORKS like it would if I just dragged and dropped the file from windows file explorer.
what I got so far:
Sub DynamicButton_StartDrag(ByVal sender As PictureBox, ByVal e As System.Windows.Forms.MouseEventArgs)
'Set variables
strFilename = Split(sender.Name, "_")(2)
strFilenameWithoutExtension = IO.Path.GetFileNameWithoutExtension(strFilename)
strFullPath = Directory_PartsAndAssembliesLibrary & strComponentsCategory & "\" & strFilename
Process.Start("explorer.exe", "/select," & strFullPath)
End Sub
Sub DynamicButton_whileDragging(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
End Sub
Sub DynamicButton_EndDrag(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
My picturebox_icon which I make programmatically has the filename in it's name and that's what I extract upon a mousedown event DynamicButton_StartDrag and then I get the full path to the file as strFullPath
Could someone at least, please, guide me to teh right approach to this problem. Practicall aplication to this is that I have a handy SOLDIWORKS files explorer form for thousands of files which makes it way easier to find what user is looking for than the windows file explorer. So Now I need to be able to drag and drop the files via the form.

Opening an Empty Directory from a Windows Form - VB.net

I'm trying to open a directory via my Windows Form created in VB.Net but every solution I've found doesn't seem to work.
Currently I'm using-
Dim path As String = Directory.GetCurrentDirectory()
Private Sub logDirBTN_Click(sender As Object, e As EventArgs) Handles logDirBTN.Click
Process.Start(path + "\Resources\Logs")
End Sub
Which returns "The system cannot find the file specified" exception. That's interesting because I know the folder is there. Furthermore this button's functionality works without any issue and from what I can tell the only difference is I'm opening a text file rather than an empty directory-
Private Sub stationListBTN_Click(sender As Object, e As EventArgs) Handles stationListBTN.Click
Process.Start("notepad.exe", path + "\Resources\StationList\StationList.txt")
End Sub
Here are all the other things I've tried-
Private Sub logDirBTN_Click(sender As Object, e As EventArgs) Handles logDirBTN.Click
'Process.Start("explorer.exe", path + "\Resources\Logs")
'Shell("explorer.exe", path + "\Resources\Logs", vbNormalFocus)
'Application.StartupPath & path + "\Resources\Logs"
'Shell(path + "\Resources\Logs", vbNormalFocus)
End Sub
Any help is greatly appreciated.
Dim MyProcess As New Process()
MyProcess.StartInfo.FileName = "explorer.exe"
MyProcess.StartInfo.Arguments = "C:\Blah"
MyProcess.Start()
MyProcess.WaitForExit()
MyProcess.Close()
MyProcess.Dispose()
Or just...
Process.Start("explorer.exe", "C:\FTP\")
Application.StartupPath is going to get you to your bin\Debug or bin\Release folder by the way, whatever folder the *.exe is in.
I'm guessing this is what you're looking for:
Process.Start("explorer.exe", Application.StartupPath & "\Resources\Logs")
Also, don't use + for joining strings. Use &
I assume you are trying to invoke Windows Explorer.
Add a trailing \ in the call to .Start
IO.Directory.CreateDirectory("C:\temp\temp")
Process.Start("c:\temp\temp\")
In the OP first example you were trying to open a file 'Logs'

VB.NET 2008 - Input to data to website controls and download results

this is my first Q on this website so let me know if I have missed any important details, and thanks in advance.
I have been asked to access a website and download the results from a user-inputted form. The website asks for a username/password and once accepted, several questions which are used to generate several answers.
Since I am unfamiliar with this area I have set up a simple windows form to tinker around with websites and try to pick things up. I have used a webbrowser control and a button to use it to view the website in question.
When I try to view the website through the control, I just get script errors and nothing loads up. I am guessing I am missing certain plug-ins on my form that IE can handle without errors. Is there anyway I can identify what these are and figure out what to do next? I am stumped.
The script errors are:
"Expected identifier, string or number" and
"The value of the property 'setsection' is null or undefined"
Both ask if I want to continue running scripts on the page. But it works in IE and I cannot see why my control is so different. It actually request a username and password which works fine, it is the next step that errors.
I can provide screenies or an extract from the website source html if needed.
Thanks,
Fwiw my code is:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Navigate("http://website.com")
'WebBrowser1.Navigate("http://www.google.com")
End Sub
Thanks for Noseratio I have managed to get somewhere with this.
Even though the errors I was getting seemed to be related to some XML/Java/Whatever functionality going askew it was actually because my webbrowser control was using ie 7.0
I forced it into using ie 9 and all is now well. So, using my above example I basically did something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'WebBrowser1.ScriptErrorsSuppressed = True
BrowserUpdate()
WebBrowser1.Navigate("http://website.com")
'WebBrowser1.Navigate("http://www.google.com")
End Sub
Sub BrowserUpdate()
Try
Dim IEVAlue As String = 9000 ' can be: 9999 , 9000, 8888, 8000, 7000
Dim targetApplication As String = Process.GetCurrentProcess.ToString & ".exe"
Dim localMachine As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim parentKeyLocation As String = "SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl"
Dim keyName As String = "FEATURE_BROWSER_EMULATION"
Dim subKey As Microsoft.Win32.RegistryKey = localMachine.CreateSubKey(parentKeyLocation & "\" & keyName)
subKey.SetValue(targetApplication, IEVAlue, Microsoft.Win32.RegistryValueKind.DWord)
Catch ex As Exception
'Blah blah here
End Try
End Sub

How to save files in vb.net

I'm trying to create an app launcher in vb.net but I do not know how to save files. Save files like the one that is executed when you run a setup for an application wherein the setup will save the application files on program files folder. I'm not trying to create a vb.net setup, because I want to run my program as portable. What I want the program to do is to place the files in their appropriate location when the user clicks a button
Here's my current code:
Public Class Nircmd
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'shutdown
System.Diagnostics.Process.Start("E:\Documents and Settings\Rew\Desktop\Shutdown.lnk")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'monitor off
System.Diagnostics.Process.Start("E:\Documents and Settings\Rew\Desktop\Monitor Off.lnk")
End Sub
End Class
-Of course it won't work if the path doesn't contain the file specified. So I want to place another button that would do just that(to save the files specified in the desired folder.
A simple syntax will do. Please
I don't quite understand, but I'll take a shot.
This will check to see if C:\foo\somefile.txt exists, and if it doesn't, create it and write some text:
If Not System.IO.File.Exists("C:\foo\somefile.txt") = True Then
Dim file As System.IO.FileStream
file = System.IO.File.Create("C:\foo\somefile.txt")
file.Close()
End If
My.Computer.FileSystem.WriteAllText("C:\foo\somefile.txt", "Some text")
If you want to copy or move a file, I think you'll want something like:
System.IO.File.Copy("C:\foo\somefile.txt", "C:\bar\somefile.txt")
or
System.IO.File.Move("C:\foo\somefile.txt", "C:\bar\somefile.txt")
I always use app.path in order to make it portable. Not everyone's computer will assign the same drive letter.

How to make my program run at startup?

I'm programming a desktop application similar to Google desktop but with my own gadget with vb.net 2008 how can i make my application when the user install it on their computer to run at the time of start up?
Let assume that my application name is windowsAplication1 and I'm using windows XP and the program will be installed on C drive?
You can add it to registry with the following code
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)
you can remove it using
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName)
The above code will add it to all users. You can add it to current user in the following key
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Or you can add a link to your application in the "Startup" folder.
I suggest you dont do it automatically it may irritate the user. I hate when apps add them automatically to Windows Startup. Give an option for the user to run the program on windows startup.
Simpley use this code:
Dim info As New FileInfo(application.startuppath)
info.CopyTo(My.Computer.FileSystem.SpecialDirectories.Programs + "\startup\myapp.exe")
hope it helps.
You Can Do this Using the code below
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' This is where you'll need to have the program
' set the check box to the previous selection that
' the user has set. It's up to you how you do this.
' For now, I'll set it as "unchecked".
CheckBox1.Checked = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' The following code is a rendition of one provided by
' Firestarter_75, so he gets the credit here:
Dim applicationName As String = Application.ProductName
Dim applicationPath As String = Application.ExecutablePath
If CheckBox1.Checked Then
Dim regKey As Microsoft.Win32.RegistryKey
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
regKey.SetValue(applicationName, """" & applicationPath & """")
regKey.Close()
Else
Dim regKey As Microsoft.Win32.RegistryKey
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
regKey.DeleteValue(applicationName, False)
regKey.Close()
End If
' Assuming that you'll run this as a setup form of some sort
' and call it using .ShowDialog, simply close this form to
' return to the main program
Close()
End Sub
Imports Microsoft.Win32
...
Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", Application.ProductName, Application.ExecutablePath, RegistryValueKind.String)
A simple method
Imports Microsoft.Win32
...
Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run",True)
regKey.SetValue(Application.ProductName, Application.ExecutablePath)
regKey.Close()
Hope it helps
Just try this code :-
FileCopy("Name.Ext", Environment.GetFolderPath(Environment.SpecialFolder.Startup) & "\Name.Ext")
Here (Name.Ext) :-
Name - Your Application's name.
Ext - The Extension, it's of-course .exe
It's the simplest and best to use.
Might be old topic but adding
Imports System.Security.AccessControl.RegistryRights
Should resolve System.Security.SecurityException: 'Requested registry access is not allowed.' trouble stated by Christhofer Natalius