opening a file from a path written in a text file? - vb.net

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)

Related

how to export listbox to notepad in vb?

i have contains listbox. how to export listbox to notepad in vb? Please advise me
Notepad typically reads and writes plain old .txt files. These can be written with a standard StreamWriter. Just iterate over your ListBox and write each item to your file:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txtFile As String = "C:\Users\mikes\Documents\Test.txt"
Using sw As New IO.StreamWriter(txtFile, False)
For Each item As String In ListBox1.Items
sw.WriteLine(item)
Next
End Using
End Sub
If you don't want to hard-code a filename, then use a SaveFileDialog to get a filename from the user.

Download, get the filename and execute the file

I am posting here to ask you for help, thank you first of all for reading my message and being able to help me. :)
So here's the problem, I have a little program that I'm creating, in this one, I integrated a button that normally allows to download a tool to run it.
However the link of the file does not point directly in .exe it is a link like this one :
mydomain.com/file/file48.php
The problem is that I manage to get the file, but this one is named file48.php and gets the extension of a php file, what I would like is to get the original name of the downloaded .exe file in order to be able to execute it within my application.
Here is the current code I'm using :
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim client As New WebClient
AddHandler client.DownloadFileCompleted, AddressOf client_DownloadFileCompleted
client.DownloadFileAsync(New Uri("https ://mydomain.com/files/index.php"), fileName, filename)
End Sub
Private Sub client_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs)
Dim client = DirectCast(sender, WebClient)
client.Dispose()
Dim filename As String = CType(e.UserState, String)
End Sub
Thanks a lot for your help, I've been searching for hours without much success...

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

Visual Basic Form. How to let users save text file where they want

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim CurrentDir As String = Environment.CurrentDirectory
Dim OutputFile2 As String = IO.Path.Combine(CurrentDir, "input.txt")
IO.File.WriteAllLines(OutputFile2, Result1.Lines)
End Sub
Right now, I have coding that saves a text file in the current directory. However, I want to have a browse button for users so that they can pick where this text file is saved. How do I proceed this?
I was trying it by my self and I'm having a trouble with using save file dialog. If you can teach me how to use a save file dialog or anyway to write save browse button, I would very appreciate it!
The documentation for the SaveFileDialog object contains an example.
Here is a tutorial on how to implement SaveFileDialog using Toolbox in Visual Studio like you mentioned. The code sample is in C# but it can be easily converted to VB.
Link: www.dotnetperls.com/savefiledialog
Private Sub button1_Click(sender As Object, e As EventArgs)
' When user clicks button, show the dialog.
saveFileDialog1.ShowDialog()
End Sub
Private Sub saveFileDialog1_FileOk(sender As Object, e As CancelEventArgs)
' Get file name.
Dim name As String = saveFileDialog1.FileName
' Write to the file name selected.
' ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test")
End Sub

Is It Possible To Create a Custom File Type For a Program In VB

Is it possible to have a custom file type that would open in a VB program.
For example: There is a text box with some text and a check box that's checked... You would save to a custom file type and when you opened the file up again, the check box would be checked and the text box would have the text. It would basically save the state of the program to a custom file type.
E.g. -> .pro, .lll, .hgy, .xyz, .abc
I'm just curious... is this possible and if so, how would I approach this?
You can do what Ichiru states with a BinaryWriter and BinaryReader which I have done with some of my projects before using an in Memory datatable and serializing it.
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Using bs As New BinaryWriter(File.Open("Mydata.xyz", FileMode.Create))
bs.Write(TextBox1.Text)
bs.Write(CheckBox1.Checked)
bs.Close()
End Using
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
If File.Exists("Mydata.xyz") Then
Using br As New BinaryReader(File.Open("Mydata.xyz", FileMode.Open))
Try
TextBox1.Text = br.ReadString
CheckBox1.Checked = br.ReadBoolean
Catch ex As EndOfStreamException
'Catch any errors because file is incomplete
End Try
End Using
End If
End Sub
End Class
But .Net has a built in Settings Class that you can use to persist your Data. It would be used like this
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
My.MySettings.Default.checkbox1 = CheckBox1.Checked
My.MySettings.Default.textbox1 = TextBox1.Text
My.MySettings.Default.Save()
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
CheckBox1.Checked = My.MySettings.Default.checkbox1
TextBox1.Text = My.MySettings.Default.textbox1
End Sub
End Class
Yes, it is possible to create your own custom filetype.
The best way to go around this kind of problem is to create a Binary Writer
In the binary writer you would write the contents of the textbox, and the state of the checkbox.
Writing:
BinaryWriter.Write("string")
BinaryWriter.Write(false)
Reading:
String str
Boolean bool
str = BinaryReader.ReadString()
bool = BinaryReader.ReadBoolean()
This is not possible unless you have your system's default application setup to open with your executable that will read this custom-extension data file.
Custom extensions can't be executed like a .exe would be, but they can be read by a .exe and used to configure settings for that particular .exe