vb.net file paths causing crashing in published exe - vb.net

i am making a 'simple' program that on button click does this:
Private Sub Button4_Click(sender As Object, e As EventArgs)
Dim proc As New System.Diagnostics.Process()
proc = Process.Start("resources\navcoder.exe", "")
proc.WaitForExit()
End Sub
all works fine when testing in visual studio but not once i publish and install, even if the resource folder is in the install directory.
if i change it to:
proc = Process.Start("c:\resources\navcoder.exe", "")
it works fine, but i obviuosly needs to have the folder there with the required files in it.
what am i doing wrong?

A lot of people seem not to realise what resources are. The whole point of adding a resource to your project is to have the data it contains compiled into your EXE. The Resources folder in your project is just a place to store the original source files. It doesn't exist as far as the application is concerned, just as your VB source code files don't exist. When you build your project, the data in those resources is compiled into your EXE so they are no longer files and can no longer be used as files.
That's why you don't embed other EXE files as resources. You could extract the resource and save it as a file first but I would recommend against that. Add a new folder to your project and add the EXE file and any dependencies to that folder and set their Build Action to Content. They will then be copied to your output folder as is. You can then execute the EXE file because it is an EXE file. You should also use Application.StartupPath as the root of the file path rather then relying on the current directory being what you think it will be.

You should always check for existance, if its a web app, would do this in the application start in the global asax. If you are running this under a different user account, check the account has permissions. Otherwise even if the directory exists, it will fail if it cant access it or does not have permissions to execute the file.
Might be able to tell you more if you give the actual exception. I.e Unauthorized or does not exist.

Related

My.Application.Info.DirectoryPath returns the source directory once in a while

I have been running a newly developed application for about five weeks. In this application, we copy some files into the bin/config folder (copy always) and then read them into the program. We need My.Application.Info.DirectoryPath to return the bin folder for the application, and that is exactly what that code is meant to do. Oddly, every fifteenth run (or randomly, I mean), this call, My.Application.Info.DirectoryPath, returns the correct folder but in the source code. That is to say, we are building a path with My.Application.Info.DirectoryPath and /config and once in a while this resolves to the config folder in the source code, not the bin folder. What could be causing this? I am using Visual Studio 2017. I am running this from the IDE every time.

Visual Basic app not seeing .CMD file in same install folder?

I've made an application that gets installed/deployed to /Program Files/STUDYvault/ called "STUDYvault Client.exe" and in the app, a button triggers/calls a .cmd that's in the same directory, called 'Scripts System.cmd'
Private Sub SynchroniseToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles MenuBackup.Click
System.Diagnostics.Process.Start("SCRIPTS SYSTEM.cmd")
End Sub
The .CMD just runs basic xcopy commands for backups. When the software is executed manually, it sees the .cmd within the same directory, however, when it is run by the system (eg startup, either by registry key or via shortcut in the startup folder) (I tried both Inno Setup AND NSIS), it crashes with an Unhandled Exception, 'Cannot find the file specified'. I'm thinking the OS is executing the .exe but running it in say /Windows or /System32 or something. It doesn't seem to be an issue with the visual studio app (it runs when executed manually, and finds the .cmd no matter what the directory) or the installer (it's basically just extracting to /Program Files and putting a link in the startup part of the registry)
My friend and I thought up a workaround, although it's quite nefarious. One could force the user to install the software to C:/Program Files/STUDYvault and have it unchangeable, and then in the app have it point to C:/Program Files/STUDYvault/Scripts System.cmd instead of just "SCRIPTS SYSTEM.cmd" - could this work also?
I'm sure I'm just missing something stupidly small, because the application fires the following code fine when installed:
Private Sub HivemindTechToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles HivemindTechToolStripMenuItem.Click
System.Diagnostics.Process.Start("http://my-website.com.au")
End Sub
EDIT:
Alright, I've developed a work-around for anyone having this issue. It seems to be rights-related or where the system is executing it from; so it's calling on the .CMD from wherever that is (most likely not its install directory) - so in-app I've had it call on:
Private Sub SynchroniseToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles MenuBackup.Click
System.Diagnostics.Process.Start("C:\\Program Files (x86)\\STUDYvault\\SCRIPTSYSTEM.cmd")
End Sub
Which is the install directory, and then in the installer (Inno Setup) I've used the following for defining the install directory under [setup]:
DefaultDirName={sd}\Program Files (x86)\STUDYvault
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
DisableDirPage=yes
Here, DefaultDirName=SD\DIRECTORY defines that it will be force-installed to Program Files (x86) (Fine for 64-bit Windows users, 32-bit users will have to deal with a second Program Files folder but x64 is the most common install in this day and age anyway so we won't have an issue there); DisableDirPage=yes hides the install page that asks the user where they want to install it - most users will leave it as-is but for the more curious user who may want to change it, this WILL break the install, seeing as it's calling on /Program Files (x86)/STUDYvault in-app.
It's a relatively dirty fix, and I feel like Dr. Evil here, but ...it works. Hopefully this will help anyone having a similar issue in the future.
Use this
Process.Start(Application.StartupPath & "\SCRIPTSYSTEM.cmd")
You can get location of the executable file by using Environment.CurrentDirectory. Using this property we can make the code works regardless of executable folder location (as long as cmd file exist in the same folder, of course). So this maybe a more proper way to get the job done :
Dim batchFilePath As String = Path.Combine(Environment.CurrentDirectory, _
"SCRIPTS SYSTEM.cmd")
System.Diagnostics.Process.Start(batchFilePath)
You may want to improve it further by adding logic to check if cmd file actually exist in the location pointed by batchFilePath (you can use System.IO.File.Exists() to do that).

open .chm file from my.resources when a buttons clicked

I already have this code from another question on this website.
Help.ShowHelp(ParentForm,
("C:\Users\Beaudean\Desktop\Help.chm"),HelpNavigator.TableOfContents, Nothing)
That works fine except i need the location of the chm help file to point to "my.resources" where it exists because i need to install my program but in that code example it only works with strings?
Thanks you :)
You cannot make this work. The .chm help file viewer is an external program, hh.exe. It doesn't know anything about managed resources, it needs a .chm file on disk.
Setup your project so that the .chm file is available in your build directory. Project + Add Existing Item and pick your .chm file. Select the added file and set its Build property to Content (so it gets installed with the rest of your files) and its Copy to Output Directory to "Copy if Newer" (so it gets copied to your build directory).
And modify your code so it can always find that file:
Dim path = System.IO.Path.Combine(Application.StartupPath, "Help.chm")
Help.ShowHelp(ParentForm, path, HelpNavigator.TableOfContents)

How can we access the files in the Data Folder when we publish the Vb.net application

I have added some files that I need to be downloaded to the Application start up path. So I set Build Action as content now the files have been copied some where
C:\Documents and Settings\TestUser.ANNAM\Local Settings\Apps\2.0\Data\HVDRBMY5.8AA\858AT9VM.TNP\test..tion_2d7cfc137d9c2c74_0001.0013_432bd4561850d290\Data
How can access file from the application. My problem since it is a dynamic path will it be same folder count so that we can use like ....\Data\ Some think like this
You can use My.Application.Info.DirectoryPath this will return the directory where the application is stored.

How can we access the files in the Data Folder when we publish the Vb.net application

I have added some files that I need to be downloaded to the Application start up path. So I set Build Action as content now the files have been copied some where
C:\Documents and Settings\TestUser.ANNAM\Local Settings\Apps\2.0\Data\HVDRBMY5.8AA\858AT9VM.TNP\test..tion_2d7cfc137d9c2c74_0001.0013_432bd4561850d290\Data
How can access file from the application. My problem since it is a dynamic path will it be same folder count so that we can use like ..\..\Data\ Some think like this
Application.UserAppDataPath gets the path for the application data of a user.
Application.StartupPath gives you the path for the executable file that started the application, not including the executable name.
Starting with one of these, you should be able to use System.IO to manipulate the paths until you get the folder where your data files are.