How to get a file from Pastebin.com and saving the file on computer in VB.NET - 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

Related

How can I move a certain file extension into one folder with VB

I am new to VB and would like to create a software that moves a certain file extension into a single folder. I have already built the code that creates a folder on the desktop when clicking the button although after that runs I need to compile a certain file such as (.png) into the folder in created.
This code creates two buttons that when pressed creates a folder called "Pictures" and "Shortcuts".
How would I go about moving all .png files from the desktop into the pictures folder?
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Pictures")
MessageBox.Show("Pictures Compiled And Cleaned")
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Shortcuts")
MessageBox.Show("Shortcuts Compiled And Cleaned")
End Sub
End Class
We'll start simple. This command will generate an array of all the PNG files' paths on the desktop
Dim filePaths = Io.Directory.GetFiles("C:\Users\bj\Desktop\", "*.png")
We can loop through this array and act on each filepath:
For Each filePath in filePaths
Dim filename = Io.Path.GetFilename(filepath)
Dim newPath = IO.Path.Combine("C:\Users\bj\Desktop\Pictures", filename)
IO.File.Move(filePath, newPath)
Next filePath
We have to pull the filename off the path and put it into a new path, then move from old to new. This I also how you rename files; have a new name in the same folder and use Move. Always use the Path class to cut and combine file paths

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

opening a file from a path written in a text file?

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim objectreader As New System.IO.StreamReader(TextBox1.Text)
System.Diagnostics.Process.Start("TextBox2.Text = objectreader.ReadToEnd()")
objectreader.Close()
End Sub
End Class
I am trying to read from a text file where i have written a path to a file. I have been able to open a file using System.Diagnostics.Process.Start() and I have been able to read from a text file using StreamReader but I could not relate to it. How do I take the text from the text file and put it in the System.Diagnostics.Process.Start("here") to open the file. I am using vb.net visual studio 2013.
Remove the quotes. You aren't providing a string literal, you're providing the result of a method call:
TextBox2.Text = objectreader.ReadToEnd()
System.Diagnostics.Process.Start(TextBox2.Text)

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

What is the file path, as a string, of a file in the application's resources?

The reason I ask is that I want to print, at run-time, a file in the application's resources, like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim printProcess As New Process
printProcess.StartInfo.CreateNoWindow = True
printProcess.StartInfo.FileName = "C:\Users\Geoffrey van Wyk\Documents\Countdown_Timer_Help.rtf"
printProcess.StartInfo.FileName = My.Resources.Countdown_Timer_Help
printProcess.StartInfo.Verb = "Print"
printProcess.Start()
End Sub
When I use "C:\Users\Geoffrey van Wyk\Documents\Countdown_Timer_Help.rtf" as the argument of FileName, it works. But when I use My.Resources.Countdown_Timer_Help, it says it cannot find the file.
No you didn't get it, that file will only be present on your dev machine. After you deploy your program, the file will be embedded in your program and cannot be printed. You'll have to write the code that saves the file from the resource to disk, then prints it. For example:
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim path As String = Application.UserAppDataPath
path = System.IO.Path.Combine(path, "Help.rtf")
System.IO.File.WriteAllText(path, My.Resources.Countdown_Timer_Help)
Dim printProcess As New Process
printProcess.StartInfo.CreateNoWindow = True
printProcess.StartInfo.FileName = path
printProcess.StartInfo.Verb = "Print"
printProcess.Start()
End Sub
Given that you have to save the resource to a file, it probably makes more sense to simply deploy the file with your app instead of embedding it as a resource and writing it to disk over and over again. Use Application.ExecutablePath to locate the file. To make that work at debug time you have to copy the file to bin\Debug. Do so by adding the file to your project with Project + Add Existing Item and setting the Copy to Output Directory property to Copy if Newer.
OK, I got it.
System.IO.Path.GetFullPath(Application.StartupPath & "\..\..\Resources\") & "Countdown_Timer_Help.rtf"