VB.NET Saving file without save dialog prompt - vb.net

I need to know how I can save a file, without using save file dialog prompt.
Currently my code is:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ProgressBar1.Value = 0
Using sfd As New SaveFileDialog
With sfd
If .ShowDialog = Windows.Forms.DialogResult.OK Then
.DefaultExt = "exe"
.Filter = "Saved.exe (*.exe)|*.exe"
'---Save File---
'---Code to pack the result with UPX packer---
ProgressBar1.Value = 100
MsgBox("Success.", MsgBoxStyle.Information)
End Sub
And I would like to know, how I can save the file with the name "Saved.exe" to the same folder where my application is, without the save file dialog's prompt.
The file needs to be saved on the same folder where the program is, with preconfigured name so the UPX packer knows what to pack.
Hopefully somebody can help me out.

There is a lot of different ways to get the directory:
My.Application.Info.DirectoryPath
Application.Current.BaseDirectory
IO.Path.GetDirectoryName(Application.ExecutablePath)
Windows.Forms.Application.StartupPath
IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().CodeBase)
Once you have the application folder, simply add the name for the filename you want to get a full filename path.

The save file dialog just allows the user to specify where they want to save the file.
If you want to save a file without this dialog you will have to specify the filename yourself.
You can use one of the following code snippets to save the file:
For a Text File:
My.Computer.FileSystem.WriteAllText("C:\SomeDir\YourFile.txt", "Text", True)
For a Binary file:
Dim fileContents() As Byte = {244, 123, 56, 34}
My.Computer.FileSystem.WriteAllBytes("C:\SomeDir\YourFile.bin", fileContents, True)
Note: These are straight from the standard snippets in Visual Studio. Press Ctrl+K, Ctrl+X then navigate to Fundamentals > FileSystem

Related

How to get a file from Pastebin.com and saving the file on computer in VB.NET

How to get a file from the Pastebin.com and the save the file to the system. In the interface there is a textbox, where a user can paste a link to a pastebin file https://pastebin.com/raw/*a-random-code*, there will be a button to start fetching the file, once the program fetches the file, the file is saved on to the computer as a downloadedtext.txt file.
I found an alternate method very easy.
Private Sub frm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filename As String
filename = "C:\Users\sample.txt"
My.Computer.Network.DownloadFile("https://pastebin.com/raw/*enter code*", filename)
End Sub

I am trying to get my vb program to read from a file

I am trying to get my program to read from a file on visual basic but it keeps saying the file does not exist, I have tried different file paths and other things but i cant seem to get it working.
my code is :
Option Strict On
Imports System.IO
Public Class MOTform
Dim custfile As StreamReader
Dim strCustArray() As String
Dim strCustDetails As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
radMOTYes.Checked = True
If File.Exists("cust_db.txt") Then
' Open the file.
custfile = File.OpenText("cust_db.txt")
Else
MessageBox.Show("cust_db.txt" & " does not exist.")
End If
strCustDetails = custfile.ReadLine()
strCustArray = Split(strCustDetails, ",")
Me.Text = strCustDetails
custfile.Close()
End Sub
go to the file you want to read from, right click on it, click Properties, copy the path from Location and insert it to the code
Your code expects the file in the same folder where your program runs because you don't have any kind of path. This is fine when you deploy your final executable because there is no BIN\DEBUG there.
In debug inside VS instead you need to have that file in that folder for the same reason. Your debugged exe runs in that folder. You can add the txt file to your project and change the property Copy to Output Directory to Copy Always.
However, keeping a data file in the same folder where your program runs is not a good practice, in particular if the file is not read only.
The operating system can prevent your application to change that file if you deploy your application in some kind of reserved folder (like C:\program files).
I suggest to use the config file adding an AppSettings key to specify the folder where you keep the file and then reading that key at runtime to build your path
For example
<appSettings>
<add key="DataFolder" value="C:\programdata\myappdatafolder"/>
</appSettings>
and then
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
radMOTYes.Checked = True
Dim fullFileName = Path.Combine(ConfigurationManager.AppSettings("DataFolder"), "cust_db.txt")
If File.Exists(fullFileName) Then
' Open the file.
custfile = File.OpenText(fullFileName)
Else
MessageBox.Show(fullFileName & " does not exist.")
End If
In this way you can change the configuration file to have your file where is most convenient for your scenario

Trouble saving ALL listbox data

Ok, so i'm trying to make an Injector. To load the DLLs, I used a File Dialog to select the DLL then write the info to a List Box. Now I want to save the data in the list box and reload the past data on the Form Load, but every method I have tried only saves the name of the DLL not the other info such as Location.
I would like to have no external files IF possible. Any solutions?
Cheers.
Edit: Source code for Open File Dialog
Private Sub OpenFileDialog1_FileOk(sender As Object, e As
System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim FileName As String = OpenFileDialog1.FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("\"))
Dim DLLfileName As String = FileName.Replace("\", "")
ListBox1.Items.Add(DLLfileName)
dlls.Add(DLLfileName, OpenFileDialog1.FileName)
End Sub

How to Make a File Name appear as Form Text

I am creating a text editor in VB.net with tabs and I am done so far. All I need is when the user saves the document or opens a document the name of the document shows up on the tab. I am using a separate tab control. I have it done but it shows the whole directory of the file. The only way to change the text on the tab is to change the text of the form I am using for the tab control to duplicate. So my code for when the user opens a file is:
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim alltext As String
filename = OpenFileDialog1.FileName
alltext = File.ReadAllText(filename)
FastColoredTextBox1.Text = alltext
Me.Text = filename
End Sub
But like I said it shows the whole directory of the file. Is there any way to make it just show the file name. The code for when the user saves the file has the same thing.
Try this:
Me.Text = System.IO.Path.GetFileName(filename)

How to set a folder as the download location folder

I have a url link that once you click on it, a txt file automatically downloads but I want to set the folder where this txt file downloads to be in the below desktop folder "C:\Users\User1\Desktop\Folder"
The below link opens the URL in a browser and downloads the file automatically to the "Downloads" folder as I am using Chrome. I would like to set the download folder via sql vb.net.
Below are the queries I tried but only the first one worked but downloaded to "Downloads" folder (very simple):
Private Sub UpdateBtn_Click(sender As Object, e As EventArgs) Handles UpdateBtn.Click
System.Diagnostics.Process.Start("URL")
End Sub
I also tried the below code but it gave me an error that I have to specify a file name - but I don't want it to work that way:
My.Computer.Network.DownloadFile("URL", "C:\Users\User1\Desktop\Folder", "", "", False, 500, True)
Another query I tested but nothing happened:
Dim wc As New Net.WebClient
Dim Path As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
wc.DownloadFileAsync(New Uri("URL"), Path)
Any help would be much appreciated.
Thank you all !
Marc
If it's just a text file you could use the WebClient to download it straight from your app and then have your app write it out to the user's hard drive (wherever you have permission of course).
Dim wc As New WebClient()
AddHandler wc.DownloadStringCompleted, AddressOf wc_DownloadStringCompleted
wc.DownloadStringAsync("URL")
Private Shared Sub wc_DownloadStringCompleted(sender As Object, e As DownloadStringCompletedEventArgs)
'add any error handling code
'...
File.WriteAllText("C:\Users\User1\Desktop\Folder\MyFile.txt", e.Result)
End Sub