Define exe output folder path - vb.net

I have .exe file which generate .csv file in the same location as .exe file.
when I run my vb.net code .csv file is not generate in .exe file location but in location where my compiled vb.net exe code is runned.
how can I define .exe output csv file folder path?
I use this code in vb.net
Dim psi As New ProcessStartInfo
psi.FileName = "E:\Downlaoder.exe"
psi.Verb = "runas"
Process.Start(psi).WaitForExit()

If your program doesn't change the startup directory by itself then you should specify the WorkingDirectory when you define the ProcessStartInfo instance
Dim psi As New ProcessStartInfo
psi.FileName = "E:\Downlaoder.exe"
psi.WorkingDirectory = "E:\" ' This if you want the file to be created in E root.

You could use Directory.GetCurrentDirectory and use it as the path to save the file to the exe directory

Related

Downloading a file into Application path

so i am using this code to download file
Dim exePath As String = Application.ExecutablePath()
My.Computer.Network.DownloadFile(TextBox2.Text, exePath & "/img.png")
where textbox2 contains url to .png file but i get this error
Note that test.exe is on Desktop.
You can do it in this way
My.Computer.Network.DownloadFile(TextBox2.Text, Environment.CurrentDirectory & "/img.png");
Environment.CurrentDirectory Will give you the current working directory.
I hope this will work for you.

How to open command prompt in specific directory with VB.Net

I need to open the command prompt programmatically but open it in a specifically directory.
In Notepad++ there is an option to open a file's directory in the command prompt.
Here is what it looks like in Notepad++.
How would you do this?
you need something like this:
Const WorkingDirectory As String = "E:\Shared" ' the directory you want to be on
Dim exePath As String = Environment.SystemDirectory & "\cmd.exe"
Dim StartInfo As New ProcessStartInfo(exePath)
Dim cmdSession As New Process
StartInfo.UseShellExecute = False
StartInfo.WorkingDirectory = WorkingDirectory ' start in this directory
StartInfo.Arguments = "/k" ' let cmd.exe remain running using /k
cmdSession.StartInfo = StartInfo
cmdSession.Start()
Regards, Paul

Zip new file in VB: File is being used by another procces

Hi folks I'm trying to zip file from a path to another path using ZipArchive and ZipFile but I can't achieve it.
zipped = "C:\Images\zip\file01.ZIP"
file = "C:\Images\file01.BAK"
Using newFile As ZipArchive = ZipFile.Open(zipped, ZipArchiveMode.Create)
newFile.CreateEntryFromFile(zipped, file, CompressionLevel.Optimal)
End Using
I'm getting the error: "C:\Images\zip\file01.ZIP" File is being used by another proccess
I will appreciate the help
Try archive mode Update instead of Create for a new entry in a zip archive from an existing file
zipped = "C:\Images\zip\file01.ZIP"
file = "C:\Images\file01.BAK"
Using newFile As ZipArchive = ZipFile.Open(zipped, ZipArchiveMode.Update)
newFile.CreateEntryFromFile(zipped, file, CompressionLevel.Optimal)
End Using

Invoking the .bat file by passing arguement from vb.net application

From Vb.Net Application, i am calling .bat file by passing SourceFile , DestinationFile. The .Bat file transfers source file to the destination folder. If i call .bat file directly from command prompt by passing arguements, file is getting transfered.
My code fails to transfer the file. I am not able to find the error where the code fails to execute the .bat file.
Dim strBatchFile As String = String.Empty
strBatchFile = AppDomain.CurrentDomain.BaseDirectory
strBatchFile = strBatchFile.Replace("\bin\Debug", "\ShellScript")
strBatchFile = strBatchFile & "callsfxcl.bat"
Dim proc As New System.Diagnostics.Process()
proc.StartInfo.UseShellExecute = False
proc.StartInfo.FileName = strBatchFile
proc.StartInfo.Arguments = String.Format("{0},{1}", strSourceFile, sSFTP)
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
proc.Start()
Batch files are run by cmd.exe. cmd /c batfile.bat. If you had of set UseShellExecute to true Windows would have done it for you. I suspect you didn't read the error.
From .NET help fror filename property.
The set of file types available to you depends in part on the value of the UseShellExecute property. If UseShellExecute is true, you are able to start any document and perform operations on the file, such as printing, with the Process component. When UseShellExecute is false, you are able to start only executables with the Process component.
Batch files are run by cmd.exe.
cmd /c batfile.bat.
So cmd is your process and /c c:\path\batch.bat is your arguments.

Extracting from your resources VB.net

I have a .zip folder in the .exe resources and I have to move it out and then extract it to a folder. Currently I am moving the .zip out with System.IO.File.WriteAllByte and unziping it. Is there anyway to unzip straight from the resources to a folder?
Me.Cursor = Cursors.WaitCursor
'Makes the program look like it's loading.
Dim FileName As FileInfo
Dim Dir_ExtractPath As String = Me.tb_Location.Text
'This is where the FTB folders are located on the drive.
If Not System.IO.Directory.Exists("C:\Temp") Then
System.IO.Directory.CreateDirectory("C:\Temp")
End If
'Make sure there is a temp folder.
Dim Dir_Temp As String = "C:\Temp\Unleashed.zip"
'This is where the .zip file is moved to.
Dim Dir_FTBTemp As String = Dir_ExtractPath & "\updatetemp"
'This is where the .zip is extracted to.
System.IO.File.WriteAllBytes(Dir_Temp, My.Resources.Unleashed)
'This moves the .zip file from the resorces to the Temp file.
Dim UnleashedZip As ZipEntry
Using Zip As ZipFile = ZipFile.Read(Dir_Temp)
For Each UnleashedZip In Zip
UnleashedZip.Extract(Dir_FTBTemp, ExtractExistingFileAction.DoNotOverwrite)
Next
End Using
'Extracts the .zip to the temp folder.
So if you're using the Ionic library already, you could pull out your zip file resource as a stream, and plug that stream into Ionic to decompress it. Given a resource of My.Resources.Unleashed, you have two options for getting your zip file into a stream. You can load up a new MemoryStream from the bytes of the resource:
Using zipFileStream As MemoryStream = New MemoryStream(My.Resources.Unleashed)
...
End Using
Or you can use the string representation of the name of the resource to pull a stream directly from the assembly:
Dim a As Assembly = Assembly.GetExecutingAssembly()
Using zipFileStream As Stream = a.GetManifestResourceStream("My.Resources.Unleashed")
...
End Using
Assuming you want to extract all the files to the current working directory once you have your stream then you'd do something like this:
Using zip As ZipFile = ZipFile.Read(zipFileStream)
ForEach entry As ZipEntry In zip
entry.Extract();
Next
End Using
Taking pieces from here and there, this works with 3.5 Framework on Windows 7:
Dim shObj As Object = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"))
Dim tmpZip As String = My.Application.Info.DirectoryPath & "\tmpzip.zip"
Using zip As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("myProject.myfile.zip")
Dim by(zip.Length) As Byte
zip.Read(by, 0, zip.Length)
My.Computer.FileSystem.WriteAllBytes(tmpZip, by, False)
End Using
'Declare the output folder
Dim output As Object = shObj.NameSpace(("C:\destination"))
'Declare the input zip file saved above
Dim input As Object = shObj.NameSpace((tmpZip)) 'I don't know why it needs to have double parentheses, but it fails without them
output.CopyHere((input.Items), 4)
IO.File.Delete(tmpZip)
shObj = Nothing
Sources: answers here and https://www.codeproject.com/Tips/257193/Easily-Zip-Unzip-Files-using-Windows-Shell
Since we are using the shell to copy the files, it will ask the user to overwrite them if already exist.