How to create .key file with specific text - vb.net

I've been trying to make a program that is able to create a file with a .key extension, which contains a 5 line text.
It is fundamental to have 5 lines, otherwise it won't work.
I use
Dim filepath As String = TextBox1.Text + "\\rarreg.key"
Dim rarreg As New IO.StreamWriter(filepath, True)
rarreg.Write(String.Join(Environment.NewLine, hiddenTxt))
The hiddenTxt contains all the text needed and it's multilined.
However, when I click on the button to call this functions, it succesfully creates the file, but it comes empty.

Try this
' Add any initialization after the InitializeComponent() call.
Dim filepath As String = ""
Dim dialog As New SaveFileDialog
dialog.DefaultExt = "key"
dialog.FileName = "rarreg.key"
dialog.InitialDirectory = "c:\temp"
Dim results As DialogResult = dialog.ShowDialog()
If results <> Windows.Forms.DialogResult.Cancel Then
filepath = dialog.FileName
End If​

Related

VB.net: overwrite everything in a text file

in my VB.net Application id like to overwrite and add new content of a text file
What Code do I need to use?
Thanks
Read (ie: load) everything in the TXT file into your program.
Dim sFullPathToFile As String = Application.StartupPath & "\Sample.txt"
Dim sAllText As String = ""
Using xStreamReader As StreamReader = New StreamReader(sFullPathToFile)
sAllText = xStreamReader.ReadToEnd
End Using
Dim arNames As String() = Split(sAllText, vbCrLf)
'Just for fun, display the found entries in a ListBox
For iNum As Integer = 0 To UBound(arNames)
If arNames(iNum) > "" Then lstPeople.Items.Add(arNames(iNum))
Next iNum
Because you wanted to overwrite everything in the file, we now use StreamWriter (not a StreamReader like before).
'Use the True to indicate it is to be appended to existing file
'Or use False to open the file in Overwrite mode
Dim xStreamWRITER As StreamWriter = New StreamWriter(sFullPathToFile, False)
'Use the carriage return character or else each entry is on the same line
xStreamWRITER.Write("I have overwritten everything!" & vbCrLf)
xStreamWRITER.Close()

Illegal Characters in path when grabbing text from a file?

I'm getting illegal characters in path, but the directory (the path) will be different for everyone, so I'm not setting a value for the "path", it's what the user chooses in the file explorer.
I haven't seen a solution for VB.net yet so here's the code I have now:
myFileDlog.InitialDirectory = "c:\"
myFileDlog.Filter = "Txt Files (*.txt)|*.txt"
myFileDlog.FilterIndex = 2
myFileDlog.RestoreDirectory = True
If myFileDlog.ShowDialog() =
DialogResult.OK Then
If Dir(myFileDlog.FileName) <> "" Then
Else
MsgBox("File Not Found",
MsgBoxStyle.Critical)
End If
End If
'Adds the file directory to the text box
TextBox1.Text = myFileDlog.FileName
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(myFileDlog.FileName)
Dim lines() As String = IO.File.ReadAllLines(fileReader)
At Dim lines() As String = IO.File.ReadAllLines(fileReader)
It breaks with the Illegal Characters in Path exception, and I'm not sure how to test where the illegal character is, because it's grabbing from your own file directory. Any help with this?
The problem originated from this line:
Dim fileReader As String = My.Computer.FileSystem.ReadAllText(myFileDlog.FileName)
fileReader takes all string contents from the corresponding file name and pass it into File.ReadAllLines method at next line, throwing ArgumentException with illegal file path message if illegal characters found inline.
Correct way to read file contents using ReadAllLines is using predefined file path or directly using FileDialog.FileName property as argument given below:
Using myFileDlog As OpenFileDialog = New OpenFileDialog()
' set dialog filters here
If (myFileDlog.ShowDialog() = DialogResult.OK) Then
If Dir(myFileDlog.FileName) <> "" Then
Dim lines() As String = File.ReadAllLines(myFileDlog.FileName)
For Each line As String In lines
' do something with file contents
Next
Else
' show "file not found" message box
End If
End If
End Using
Since ReadAllLines already being used to fetch all file contents, usage of ReadAllText may be unnecessary there.

new folders from a list, from a text file in vb.net

I want to be able to create new folders from a list that is stored in a text file.
The names are stored like
test1
test2
test3
so my code so far, loads the path to create the new folders, (which is the oldest folder in the given parent folder) stored in another text file "Foldercreation.txt"
then open the file with the names of the folders I want to create, "Folderstocreate.txt" and stores them all in filereader2.
but then when trying to create the folders for each line nothing happens.
My current code;
Dim fileReader, filereader2 As System.IO.StreamReader
Dim stringreader, parfolder As String
Dim path, foldername As List(Of String)
Dim count As Byte
If MsgBox("Are you sure you want to create these folders?,
Before clicking yes, make sure EVERYONE is out of paperport & you have entered the correct numbers.", MsgBoxStyle.YesNo, "WARNING!") = MsgBoxResult.Yes Then
If strnumbx.Text = "" Then
MsgBox("You have not entered a start number for the folders.", MsgBoxStyle.OkOnly, "Error")
End If
'Loads a text file at the given location, to read to.
fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\Data\Test\Foldercreation.txt")
'Set stringreader as the read line from the file
stringreader = fileReader.ReadLine()
path = System.IO.Directory.GetDirectories(stringreader).ToList
path.Sort()
count = path.Count - 1
parfolder = path(count)
'System.IO.Directory.CreateDirectory(parfolder & "\test")
filereader2 = New StreamReader("C:\Data\Test\Folderstocreate.txt", True)
filereader2.ReadToEnd()
For Each line In filereader2.ReadToEnd()
System.IO.Directory.CreateDirectory(parfolder & fileReader.ReadToEnd(count - 1))
count = count + 1
Next
End If
fileReader.Close()
filereader2.Close()
This function would do it but you may want to put in some exception handling.
Directory.CreateDirectory will create all parent folders if they don't exist.
Private Sub CreateAllDirectories(ByVal strFileList As String)
Dim strDirectories As String() = File.ReadAllLines(strFileList)
For Each strDirectory As String In strDirectories
If Not Directory.Exists(strDirectory) Then
Directory.CreateDirectory(strDirectory)
End If
Next
End Sub

SendMail Help in VB

I developed a sendmail program for support which works great. What I am trying to do now is add a history page, which will show my users what support requests they have sent previously. I plan on use My.Settings to save the each email sent from my program.
Here is my code for the SendMail button which sends me mail:
If TextBox1.Text = "" Then
MsgBox("Please describe the issue you're having Bender, I'm not a mindreader!")
Exit Sub
Else
lblpleasewait.Visible = True
delay(2000)
Dim Recipients As New List(Of String)
Recipients.Add("johndoe#yahoo.com")
Dim FromEmailAddress As String = Recipients(0)
Dim Subject As String = "IT Help!"
Dim Body As String = TextBox1.Text
Dim UserName As String = My.Settings.txtboxUN
Dim Password As String = My.Settings.txtboxPW
Dim Port As Integer = My.Settings.txtboxSMTPPort
Dim Server As String = My.Settings.txtboxSMTP
Dim Attachments As New List(Of String)
MsgBox(SendEmail(Recipients, FromEmailAddress, Subject, Body, UserName, Password, Server, Port, Attachments))
lblpleasewait.Visible = False
TextBox1.Text = ""
TextBox1.Focus()
'This is where the beginning of my code is to send it to the history form.
Dim str(2) As String
Dim itm As ListViewItem
str(0) = Today + " - " + TimeOfDay
str(1) = Body
itm = New ListViewItem(str)
GTSMailHistory.ListView1.Items.Add(itm)
My.Settings.logDate = str(0)
My.Settings.logIssue = str(1)
My.Settings.Save()
End If
As you can from the above code, the last few lines is where I add the Body of the email and the time of day and add it to my listview I have on another form (GTSMailHistory).
My problem is this, that code above sending it over to the other form is saving, but is overwritten with each new email. Its basically not appending new emails to the list, just writing over the first.
The only code I have on the history form is on the LOAD function which is below:
Private Sub GTSMailHistory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim str(2) As String
Dim itm As ListViewItem
str(0) = My.Settings.logDate
str(1) = My.Settings.logIssue
itm = New ListViewItem(str)
ListView1.Items.Add(itm)
End Sub
I can't leave comments yet so if someone could move this it'd help.
Are you recreating your GTSMailHistory object each pass instead of just once at startup?
Also when saving the date/body to My.Settings you are overwriting the existing values, do you not want these to be lists instead?
In response to your comment:
Joiner, i do want it to be a list. Can i save a listview to my.settings so it remains persistent upon next program launch?
You can use My.Settings to store variables of different types including lists.
In Visual Studio go to:
Project -> Properties -> Settings (Tab) -> Add a setting and its type (browse for more).
See here for an example.
Found sort of an answer. Not really want i want but will suffice right now. I can write this to a textfile and use streamreader to load it to listview.
The below code adds the body and date to listview:
Static i As Integer = 0
Dim newItem As New ListViewItem(Today + " - " + TimeOfDay) '// add text Item.
newItem.SubItems.Add(TextBox2.Text) '// add SubItem.
history.ListView1.Items.Add(newItem) '// add Item to ListView.
i += 1
The below code loads the saved textfile
ListView1.View = View.Details : ListView1.Columns.Add("Date") : ListView1.Columns.Add("Issue")
If IO.File.Exists(myCoolFile) Then '// check if file exists.
Dim myCoolFileLines() As String = IO.File.ReadAllLines(myCoolFile) '// load your file as a string array.
For Each line As String In myCoolFileLines '// loop thru array list.
Dim lineArray() As String = line.Split("#") '// separate by "#" character.
Dim newItem As New ListViewItem(lineArray(0)) '// add text Item.
newItem.SubItems.Add(lineArray(1)) '// add SubItem.
ListView1.Items.Add(newItem) '// add Item to ListView.
Next
End If
On form close it saves my entries:
Dim myWriter As New IO.StreamWriter(myCoolFile)
For Each myItem As ListViewItem In ListView1.Items
myWriter.WriteLine(myItem.Text & "#" & myItem.SubItems(1).Text) '// write Item and SubItem.
Next
myWriter.Close()

How to use the multiselect feature in open file dialog box

What code will I put in the open file dialog box_fileok if the multi select feature is enabled.
I currently have this code, but it only shows in the textbox the last file that has been selected.
Dim strm As System.IO.Stream
strm = OpenFileDialog3.OpenFile()
TextBox3.Text = OpenFileDialog3.FileName.ToString()
If Not (strm Is Nothing) Then
//insert code to read the file data
strm.Close()
MessageBox.Show("Done!")
End If
Use the FileNames property. It returns a string array containing all selected files.
TextBox3.Text = String.Join(Environment.NewLine, OpenFileDialog3.FileNames)
' Displays each filename on a separate line