how to execute a batch file using a relative path added to the project - vb.net

i have created a batch file and have added it to the project using add items. Basically what i am aiming at is to execute this file on a button click action.
I am using System.Diagnostics.Process.Start("hello.bat") command to run this file
i have changed the build action to resource for this batch file.
But when i run this program, it is not able to locate the batch file.
I am required to give a relative path as the path my vary from machine to machine. how can i make this file accessable using a relative path?

Resource puts it inside of your EXE as data. You can google how to write a vb.net resource to a file, use the io tempfilename function to get a tempfile and use that (appending .bat), then run the batchfile from the name you gave it.
If you can ship the .bat with your EXE, this is convenient for debugging and production:
* Put the batchfile in the BIN subdir (debug or release) with your exe. May have to click 'show all files' in project explorer to see these dirs. Right click the .bat and pick 'include in project'. Don't make it a resource.
Run it using application.startuppath & "\" & batfilename. (application.startuppath is only in winforms. You can google 'how to get exe path in vb.net console app' etc. if you need another way).

Related

How to access a file within program file without the full path

I am using c++/winrt within a WinUI3 project and I am trying to have the application pull up a text file where users can select an option from it. The file I am trying to reach is in the project folder and included in the project. Currently, I have the file path set to my device so it pulls the text file from my directory. I want the application to be able to read from the program files instead of the long file path that is currently coded. I tried to change the relative path of the file and used the fstream function to read the file. I also tried just using "Aircrafts/Aircrafts.txt" but that did not work either.
Here is a snippet of the code.
fstream aircraftFile;
string info;
void MainWindow::loadPlatformData()
ifstream aircraftFile; //File Object for list
aircraftFile.open("C:\\Users\\TimmyK\\Documents\\GitHub\\sentinel3\\Sentinel3\\Aircrafts\\Aircrafts.txt", ios::in);
My guess is that you are writing a UWP project which is why the full-path access to your file fails. UWP's have restricted access to the file system per Microsoft Docs.
For a UWP project, you mark the file as Content Yes in the file properties so it's placed into your application's layout package. Normally the 'current working directory' is pointing to your packaged program's installed location. You can get a full path to this directory using:
auto installdir = Windows.ApplicationModel::Current().InstalledLocation();
std::wstring str = installdir.Path().c_str();
For Win32 "classic" desktop applications, there's no specific packaging or installation solution so there's lots of different ways to do it. In Visual Studio when you start the debugger or run a program, the "current working directory" is going to be the project directory but if you run the EXE from the command-line, it will be in a different directory.
To find the running EXE's directory per IInspectable, use GetModuleFileName:
wchar_t exePath[MAX_PATH] = {};
DWORD nc = GetModuleFileNameW(nullptr, exePath, MAX_PATH);
if (nc > MAX_PATH || nc == 0)
{
// Error condition
}
A typical pattern is to look in the EXE folder, then 'walk up' the directory to find asset files. This is what we do a lot in DirectX samples for Win32. See FindMedia.

how to install my customs' cuix,vlx and mnr files to autocad supported files programmatically

I would like to know how I can load my own custom autocad files to the autocad working supported files without options inside Autocad software, but programmatically.
I have these following files that I want to load to supported files of autocad programatically ,
.cuix file
.vlx file
.mnl file
.mnr file
.fas file.
I tried with inno setup.
Honestly, I don't know how to do.
Please guide me or teach me how to do.
As per my understanding, you can achieve this stuff by Autoloading those File, while the AutoCAD is started, there is plenty amount of solution available on internet you may check this link1 Link2
Or you can refer below step(Before following this step read above link)
1.Write a function to load the required file as you mention in Question
(defun Load_File()
;To load CUIX file "<..MyPath.../MYMENU.CUIX>" replace this with you CUIX file path
(command "_MENULOAD" "<..MyPath.../MYMENU.CUIX>" "")
;To load VLX file "<..MyPath.../MY.VLX>" replace this with you VLX file e path
(command "_appload" "<..MyPath.../MY.VLX>")
;Loading a MNU file overwites the corresponding .MNR, .MNS and .MNC files. Keep in mind that if you make any custom toolbars ;and/or buttons using the graphical on-screen method - they will be wiped when you load the MNU
;(I am not sure about MNL/MNR file loading you may try this )
;To load MNLfile "<..MyPath.../MY.MNL>" replace this with you MNL file e path
(command "_appload" "<..MyPath.../MY.MNL>")
;To load Fas file
(load "<your .Fas file path/my.fas>"
)
(load_File)
2.save this file with name as Load_file.Lsp on trusted path (ie "c:/trusted path/...../Load_File.lsp")
(if lisp file is save in trusted path of AutoCAD so AutoCAD not show pop-up while lisp file is load )
3.make new lisp file so it can Autoload as AutoCAD is started with name "acad.lsp" put below code inside file (this code say that load our first "Load_File.lsp" file)
(load "c:/trusted path/...../Load_File.lsp")
this lisp file must be save in install directory of autocad (Ex. "C:\Program Files\Autodesk\AutoCAD 2018") this step is for automatically load "acad.lsp"
As soon as acad.lsp is loaded all your files are load in Autocad.
InnoSetup is a very good choice for You. I use it for few years and it let me do anything I need.
If You have InnoSetup installed, just click File->New and InnoSetup Script Wizard will guide you through the process of creation script. One of the steps is Application Files, where just select Your files mnu, cuix, fas whatever You like
After that compile ( just one click ) and You have Your setup.exe
Using Innosetup You may install files, but also manipulate OS registry - which may be helpful for Your application, but also set Acad supported files paths.
here
You can find more details about how to load Your application to Acad after is installed.

vb.net file paths causing crashing in published exe

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.

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