Inputbox input to streamwriter file - vb.net

So, in my assignment, we're supposed to create phone directories in the Directories.txt file and then change the listings in the directories. I made a blank directories.txt file and placed that in the debug folder. I also have created a button titled Create a New Phone Directory. When a user clicks on it, the inputbox shows up prompting the user to title the new directory. I am wondering how to get the results from that inputbox that the user typed and use that to create a new directory file in directories.txt and display it in a listbox. I think I have to use stream writer but every time I try, the result in the listbox shows up as system.IO.streamwriter.
This is my current code:
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
inputDirectories = InputBox("Please Enter the Name of the New Directory")
If inputDirectories Is "" Then
MessageBox.Show("Invalid Directory Name")
End If
Dim fileDirectories As IO.StreamWriter = IO.File.CreateText(inputDirectories)
fileDirectories.WriteLine(inputDirectories)
End Sub
The assignment instructions say to use to write line method to add the name of the new file to the directories.txt file but I am totally lost on how to do this.
Any help would be appreciated!

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
'
Dim path As String = "c:\temp\MyTestDirectory.txt"
Dim fileDirectories As System.IO.StreamWriter
Dim inputDirectory As String = ""
'
inputDirectory = InputBox("Please Enter the Name of the New Directory")
If inputDirectory = "" Then
MessageBox.Show("Invalid Directory Name")
Else
If System.IO.File.Exists(path) = False Then
'The file does not exist so create a new file & add the inputted data
fileDirectories = System.IO.File.CreateText(path)
fileDirectories.WriteLine(inputDirectory)
fileDirectories.Flush()
fileDirectories.Close()
Else
'The file exists so append file with the inputted data
fileDirectories = System.IO.File.AppendText(path)
fileDirectories.WriteLine(inputDirectory)
fileDirectories.Flush()
fileDirectories.Close()
End If
End If
'
End Sub
Sub ReadDataBackNow()
' Open the file to read from one line at a time
Dim path As String = "c:\temp\MyTestDirectory.txt"
Dim DataStreamIn As System.IO.StreamReader = System.IO.File.OpenText(path)
Dim TextLines As String = ""
'
Do While DataStreamIn.Peek() >= 0
TextLines = TextLines & DataStreamIn.ReadLine()
Loop
DataStreamIn.Close()
MsgBox(TextLines)
End Sub
UPDATE
Update to answer additional question.
In your button click event add the following line
Listbox1.Items.Add(inputDirectory)
Add the line AFTER the inner IF THEN block so your code would like this
If System.IO.File.Exists(path) = False Then
'The file does not exist so create a new file & add the inputted data
fileDirectories = System.IO.File.CreateText(path)
fileDirectories.WriteLine(inputDirectory)
fileDirectories.Flush()
fileDirectories.Close()
Else
'The file exists so append file with the inputted data
fileDirectories = System.IO.File.AppendText(path)
fileDirectories.WriteLine(inputDirectory)
fileDirectories.Flush()
fileDirectories.Close()
End If
Listbox1.Items.Add(inputDirectory)
Note that you will need several files to answer your question, so you may end up with something like
Directories.txt (contains list of directories)
Friends_Directory.txt
Workmates_Directory.txt
Family_Directory.txt
Friends_Directory.txt (contains list of friends)
Bob 1234567890
Angela 2345678901
Steve 3456789012
Ahmed 4567890123
Fatima 5678901234
Workmates_Directory.txt (contains list of workmates)
CEO_Alan 0987654321
Manager_Daisy 0876543219
Foreman_Judy 0765432198
Colleague_Jill 0654321987
Family_Directory.txt
Bro_Malcolm 1122334455
Sis_Alisha 2233445566
Moms 3344556677
Pops 4455667788
Uncle_Ben 5566778899
Aunty_Sarah 6677889900

Related

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

Vb.net Get the text from Textbox inserted line by line in other Textboxes

Main PageI made a program that gives the user the possibility to insert text into text boxes and then it gets saved as a Text-file. Now I want to give the user a possibility to read the values from file back to the Text boxes. I though about importing the whole text into a big multi-line-textbox( I called it "mothertext") and from there import it into other textboxes..
I think about something that works like:
textbox1.text = mothertext.text.line (1)
textbox2.text = mothertext.text.line (2)
textbox3.text = mothertext.text.line (3)
Is there a way that works that way or similar?
Thanks a lot if you can help me :)
"Für später speichern"= Save for later/later editing
"Öffnen"= Open( *.txt file)
Thats the last function, seems so easy but... yah
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
Static count As Integer
count = count + 1
OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog1.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
If (OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim read As IO.StreamReader
read = IO.File.OpenText(OpenFileDialog1.FileName)
TextBox17.Text = read.ReadToEnd()
read.Close()
Dim readLines() As String = IO.File.ReadAllLines(OpenFileDialog1.FileName)
TextBox1.Text = readLines(0)

VB.NET stopping strings being automatically split by spaces

I am building a program in VB that populates a listbox with all the songs in a chosen folder and opens them with media player when you click on them in the list. All of that works but my problem is that pretty much no matter what I do (I've tested this with WMP, VLC, and Winamp) it splits up the argument string by spaces, for example if the file path is C:\Program Files (x86)\Test song.mp3, it will try to open in sequence: C:\Program, then Files, then (x86)\Test, then song.mp3. It works on file paths containing no spaces, but else it always fails.
Private Sub SongListBox_Click(sender As Object, e As EventArgs) Handles SongListBox.Click
Dim song As String = SongListBox.SelectedIndex
If SongListBox.SelectedIndex = "-1" Then
Else
Dim SongPath As String = Pathlist.Items.Item(song)
Dim SongProcess As New ProcessStartInfo
SongProcess.FileName = "C:\Program Files\Winamp\winamp.exe"
SongProcess.Arguments = SongPath
SongProcess.UseShellExecute = True
SongProcess.WindowStyle = ProcessWindowStyle.Normal
MsgBox(Pathlist.Items.Item(song), MsgBoxStyle.Information, "Song")
Dim SongStart As Process = Process.Start(SongProcess)
SongListBox.Select()
End If
End Sub

Open, Launch or Show a file for the user to read or write in vb.net

It sounds very simple but I have searched and cannot seem to find a way to open a log file which the user just created from my windows form app. The file exits I just want to open it after it is created.
I have a Dim path As String = TextBox1.Text and once the user names and clicks ok on the savefiledialog I have a msgbox that says "Done" and when you hit OK I have tried this
FileOpen(FreeFile, path, OpenMode.Input) but nothing happens. I just want it to open the log and show it to the user so they can edit or save it again or anything.
This is where I got the above code.
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.filesystem.fileopen.aspx
Searching is difficult because everyone is trying to "Open" a file and process it during runtime. I am just trying to Show a file by Launching it like someone just double clicked it.
Here is the entire Export Button click Sub. It basically writes listbox items to file.
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim sfd As New SaveFileDialog
Dim path As String = TextBox1.Text
Dim arypath() As String = Split(TextBox1.Text, "\")
Dim pathDate As String
Dim foldername As String
foldername = arypath(arypath.Length - 1)
pathDate = Now.ToString("yyyy-MM-dd") & "_" & Now.ToString("hh;mm")
sfd.FileName = "FileScannerResults " & Chr(39) & foldername & Chr(39) & " " & pathDate
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
sfd.Filter = "Text files (*.txt)|*.txt|CSV Files (*.csv)|*.csv"
sfd.ShowDialog()
path = sfd.FileName
Using SW As New IO.StreamWriter(path)
If CkbxFolder.Checked = True Then
SW.WriteLine("Folders")
For Each itm As String In ListBox1.Items
SW.WriteLine(itm)
Next
End If
If CkbxFiles.Checked = True Then
SW.WriteLine("Files")
For Each itm As String In ListBox2.Items
SW.WriteLine(itm)
Next
End If
End Using
MsgBox("Done...")
FileOpen(FreeFile, path, OpenMode.Input) 'Why can't I open a file for you...
End Sub
Do not use the old VB6 methods. They are still here for compatibility reason, the new code should use the more powerful methods in the System.IO namespace.
However, as said in comments, FileOpen don't show anything for you, just opens the file
You coud write
Using sr = new StreamReader(path)
Dim line = sr.ReadLine()
if !string.IsNullOrEmpty(line) Then
textBoxForLog.AppendText(line)
End If
End Using
or simply (if the file is not too big)
Dim myLogText = File.ReadAllText(path)
textBoxForLog.Text = myLogText
As alternative, you could ask the operating system to run the program associated with the file extension and show the file for you
Process.Start(path)
To get the same behavior as if the user double-clicked it, just use System.Diagnostics.Process, and pass the filename to it's Start method:
Process.Start(path)
This will open the file using whatever the default application is for that filename based on its extension, just like Explorer does when you double-click it.

code that doesn't work...Kill-process , search-for-file , streamwrite on file searched file

can you help me with that code ?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As String = "C:\Users\Andy\Documents\Visual Studio 2008\Projects\minecraft srv\"
For Each app As Process In Process.GetProcesses
If app.ProcessName = "notepad" Then
app.Kill()
End If
Next
Dim result As String
Dim servprop() As String
servprop = System.IO.Directory.GetFiles(x, "server.*")
For Each file In servprop
result = Path.GetFileName(file)
Next
Dim z As String = "C:\Users\Andy\Documents\Visual Studio 2008\Projects\minecraft srv\" & result.ToString
Dim t As StreamWriter = New StreamWriter(z)
t.WriteLine(TextBox1.Text.ToString)
t.Close()
End Sub
so... I got a button (button1) that finds if notepad is opened and kills it.
Then it searches for "server.Properties" in "x" location
if server.properties is found , then "result" will get his name (server)
"z" is the file location where streamwriter must write the text from textbox1 .
And it doesn't work... streamwirter is not writing on server.properties ... why ?
mention : I'm just a kid :D and i'm trying to learn by myself visual basic .
If you have only one file called "server.properties" then you could remove all the code that search for this file and write it directly.
Dim z As String
z = System.IO.Path.Combine(x, "server.properties")
Using t = New StreamWriter(z)
t.WriteLine(TextBox1.Text.ToString)
t.Flush()
End Using
Regarding the error, encapsulating the writing code with a proper using statement ensures a complete clean-up. Also adding a call to Flush() is probably not necessary, but doesn't hurt.