[VB.NET]Transferring data from 1 file to another - vb.net

Basically I want to take My Client.
Then for example Lets say in my client I have "A = 1"
Then my 2nd file which has random data in it.
So Client= My Client
File = The File which I want in the end result
How could I Inject "A = TextBox1.Text" from Client to File.
I heard it's called "End Of File" or something like that.
Any help please?

Use System.IO.File in combination with StreamWriter.
File.CreateFile() helps you create files. Check if the following sample helps you (This is not written to solve your exact expectation, but gives you the idea of available options)
Dim fs as new FileStream("YourFile.bin", FileMode.Create)
Dim writer as New BinaryWriter(fs)
For i as Integer = 0 To 10
writer.Write(CInt(i))
Next
writer.Close()
fs.Close()
FileStream.CreateText() method helps you create a Text stream and you can use this to write your textbox content into file.

Related

Write to a text file while is it open and visible

I'm sure this is really simple, but has had me stumped for a while!
I need to show the user a text file, with lines being written to it as my program executes. Stuff like "Working on this file - Successful!". Another forum thread has helped me to allow the text file to be accessed by multiple processes, but now when I open the text file using Process.Start, it doesn't show the lines that are being written using the StreamWriter. Any help very much appreciated.
Code:
Dim ExportLog As String = "C:\ExportLog.txt"
If System.IO.File.Exists(ExportLog) Then
System.IO.File.Delete(ExportLog)
End If
Using writeStream = System.IO.File.Open(ExportLog,
FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.ReadWrite), Write As New StreamWriter(writeStream)
Write.WriteLine("Starting")
End Using
Dim MyLog As Process = Process.Start(ExportLog)

Counting each line in a textfile

So I'm a bit stuck with this. Basically I've got a text file in a directory and in that text file, I've added different text in each of its line. I'm trying to use this to count how many lines there are in that text file. I'm using IsolatedStorageFile to do this and this is how I'm accessing it.
So far I've got this but It's not working:
Using isoComments As IsolatedStorageFileStream = New IsolatedStorageFileStream("textfile1.txt", FileMode.Open, isoStore)
Using readerComments As StreamReader = New StreamReader(isoComments)
For Each line In readerComments.ReadLine
MessageBox.Show(line.ToString.Count)
Next
End Using
End Using
The result is not the amount of lines instead, the amount of characters in each line. This is not what I want I want the number of lines in that particular textfile. Is anyone able to help me with this.
Something like this:
Dim iLineCount As Integer
iLineCount = 0
Using isoComments As IsolatedStorageFileStream =
New IsolatedStorageFileStream("textfile1.txt", FileMode.Open, isoStore)
Using readerComments As StreamReader = New StreamReader(isoComments)
For Each line In readerComments.ReadLine
iLineCount=iLineCount+1
Next
MessageBox.Show(iLineCount)
End Using
End Using
So I managed to solve this problem. My original idea was to get the amount of times a textfile was edited and so I figured adding something in each line of that textfile and counting each line would help but i couldn't find a way to it.
What I did was I added a counter which increases by 1 everytime it saves and then when it's read +1 is added to it. That way I have an exact number of times it was edited because everytime the textfile is edited, like mentioned before, it increases like a counter by 1. so if the user edited the textfile about 12 times then there will be the number 12 in the file and that will be read when the ser decides to edit the textfile again, once he saves +1 to the 12 will be added maing it 13.
I used this method to do the increment by one:
Using isoStream As IsolatedStorageFileStream = New IsolatedStorageFileStream("textfile.text", FileMode.CreateNew, isoStore) Using writer As StreamWriter = New StreamWriter(isoStream)
Dim i As Integer
i += 1
writer.WriteLine(i)
End Using
End Using

Simple File I/O in Visual Basic 2013

I want to learn how to write and read data from files using Visual Basic Studio Express 2013 for Windows Desktop.
Specifically, I have a class called Pilots. The class includes text of a first and last name, and integers representing rank, skill, missions and status.
There is an ArrayList called Squadron that contains 18 (at start) Pilots.
What I want to do is save and then load all the data in Squadron to a file. And I'm not sure how to get it to work properly. I've read numerous books and sites, but somewhere along the line, I'm just not comprehending how to get it to work correctly.
At this point, I've been able to write the data to a file.... but I'm having no luck getting it back out of the file.
To write it out... I'm using this code:
Dim currentpilot As New Pilot("John", "Doe")
' Create the BinaryWriter and use File.Open to create the file.
Using writer As BinaryWriter = New BinaryWriter(File.Open("squadron.dat", FileMode.Create))
' Write each integer.
writer.Write(SquadronList.Count)
For index = 0 To SquadronList.Count - 1
currentpilot = CType(SquadronList(index), Pilot)
writer.Write(currentpilot.pFirstName)
writer.Write(currentpilot.pLastName)
writer.Write(currentpilot.pSkill)
writer.Write(currentpilot.pRank)
writer.Write(currentpilot.pStatus)
writer.Write(currentpilot.pMissions)
writer.Write(currentpilot.pKills)
Next
End Using
The writer.Write(SquadronList.Count) line is there to record the number of actual Pilot records in the file. I don't know if that's necessary or not.
The file does write to disk and just looking at it with notepad, it does seem to have the correct data.
The problem is getting it back out. This code fails quickly...
Using reader As BinaryReader = New BinaryReader(File.OpenRead("squadron.dat"))
Dim index As Integer = reader.ReadInt32
For counter = 0 To index - 1
currentpilot.pFirstName = reader.ReadString
currentpilot.pLastName = reader.ReadString
currentpilot.pSkill = reader.ReadInt16
currentpilot.pRank = reader.ReadInt16
currentpilot.pStatus = reader.ReadInt16
currentpilot.pMissions = reader.ReadInt16
currentpilot.pKills = reader.ReadInt16
SquadronList.Add(currentpilot)
Next
End Using
So... any suggestions or guidance will be appreciated!

Patching System in VB

I am trying to make a patching system or an updater if you will for my program. What I want it to do is download the patch list which is in a specific form in the text file.
Like this:
1 http://127.0.0.1:8080/patches/test1.rar
2 http://127.0.0.1:8080/patches/test2.rar
3 http://127.0.0.1:8080/patches/test3.rar
It's a tab in between 1 and the link.
And then I want it to check if the version number of the launcher or program at the moment matches the LAST patch in the PatchList.txt file. The last patch is version 3 and the launcher version at the moment is 0.
So if it doesn't match then it should start downloading one by one the patches. From patch 1 to patch 3.
I thought a loop statement would be best for it so I came up with this idea:
Make it download the PatchList.txt and then read each line individually. After it reads a line it will split where tab is so there will be two variables Version and PatchURL.
It will then check Version to see if it matches the launcher version and if it doesn't then it will download files from the PatchURL and apply it and then set the launcher version to the version of the current patch and then check again, if the version number and if it does match then there will be no updates so it will finish.
I don't know exactly how to put it into code. I have been trying for ages to be able to Download the txt file and read it line by line but I haven't been able to. I have tried many different things from StreamReader to StringReader and a bunch of other stuff.
I'd appreciate any help. This is very frustrating for me.
If you just want to download the file and read it in again, it's as easy as
My.Computer.Network.DownloadFile("http://your_url.txt", "local_tmp_path")
If you don't want to save the file on disk but read it in memory, you could use something like this:
Dim httpRequest = DirectCast(WebRequest.Create("http://your_url.txt"), HttpWebRequest)
Dim httpResponse = DirectCast(httpRequest.GetResponse(), HttpWebResponse)
Dim line As String
Using responseStream = httpResponse.GetResponseStream(), sr = New StreamReader(responseStream)
Do
line = sr.ReadLine()
If line IsNot Nothing Then
Dim s = line.Split(new String() {" "}, StringSplitOptions.RemoveEmptyEntries)
Dim version = s(0)
Dim path = s(1)
'Do something'
End If
Loop Until line Is Nothing
End Using
If you try to implement an update system for your software, better have a look at ClickOnce first.
What do you mean by download?
I would try the solution mentioned here.
How to read a specific line from a text file in VB
Otherwise, I would reccommend using XML for the version file and just deserializing it.

Using a .txt file Resource in VB

Right now i have a line of code, in vb, that calls a text file, like this:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("data5.txt")
data5.txt is a resource in my application, however the application doesn't run because it can't find data5.txt. I'm pretty sure there is another code for finding a .txt file in the resource that i'm overlooking, but i can't seem to figure it out. So does anyone know of a simple fix for this? or maybe another whole new line of code? Thanks in advance!
If you added the file as a resource in the Project + Properties, Resources tab, you'll get its content by using My.Resources:
Dim content As String = My.Resources.data5
Click the arrow on the Add Resource button and select Add Existing File, select your data5.txt file.
I'm assuming that the file is being compiled as an Embedded Resource.
Embedded Resources aren't files in the filesystem; that code will not work.
You need to call Assembly.GetManifestResourceStream, like this:
Dim fileText As String
Dim a As Assembly = GetType(SomeClass).Assembly
Using reader As New StreamReader(a.GetManifestResourceStream("MyNamespace.data5.txt"))
fileText = reader.ReadToEnd()
End Using
First, go to project resources (My Project --> Resources), and drag-and-drop your file, say "myfile.txt", from the file system to the resourses page.
Then:
Imports System.IO
...
Dim stream As New MemoryStream(My.Resources.myfile)
Dim reader As New StreamReader(stream)
Dim s As String = reader.ReadToEnd()