writing text to a "unknown" file

Johnson

Well-known member
Joined
Mar 6, 2009
Messages
158
Programming Experience
Beginner
hello, firstly i am a complete beginner to vb.net.

My problem is i want to be able to write a url to a file. But there are a few problems.

First one is the file is "unknown" to windows. Its the windows host file. which has no extension.

second one is i need to write a line 21 lines down (making sure it does not over write any previously stored text. The line should look something like this

127.0.0.1 www.007guard.com

Can any one help me write this bit of code please. Iv searched gor ages for this but cant find it :(

her eis my code to get an idea

VB.NET:
Public Class Form1
    Inherits System.Windows.Forms.Form


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Add Version(s) to combo box
        cmbVersion.Items.Add("Windows NT/2000/XP Pro")
        cmbVersion.Items.Add("Windows XP Home")
        cmbVersion.Items.Add("Windows 95/98/Me")
        cmbVersion.Items.Add("Vista Home/Pro")

        'Select second option as default
        cmbVersion.SelectedIndex = 1
    End Sub

    Private Sub btnPatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatch.Click
        If cmbVersion.SelectedIndex = -1 Then
             MsgBox("Stupid", MsgBoxStyle.Critical)
            cmbVersion.Focus()
        Else
            If cmbVersion.SelectedIndex = 0 Then
                'User slected xp pro
                'Check file exists
                If Dir("c:\winnt\system32\drivers\etc\hosts") <> "" Then
                    MsgBox("Patched")
                Else
                    MsgBox("Can not Patch. File is missing", MsgBoxStyle.Critical)
                End If
            ElseIf cmbVersion.SelectedIndex = 1 Then
                'User selected xp home
                'Check file exist
                If Dir("c:\windows\system32\drivers\etc\hosts") <> "" Then


                    MsgBox("Patched")
                Else
                    MsgBox("Can not Patch. File is missing", MsgBoxStyle.Critical)
                End If

            ElseIf cmbVersion.SelectedIndex = 2 Then
                'User selected 95/98/Me
                'Check file exists
                If Dir("c:\windows\hosts") <> "" Then
                    MsgBox("Patched")
                Else
                    MsgBox("Can not Patch. File is missing", MsgBoxStyle.Critical)
                End If
            ElseIf cmbVersion.SelectedIndex = 3 Then
                'User selected vista
                'Check file exist
                If Dir("C:\Windows\System32\Drivers\etc") <> "" Then
                    MsgBox("Patched")
                Else
                    MsgBox("Can not Patch. File is missing", MsgBoxStyle.Critical)
                End If
            End If

        End If

    End Sub

    Private Sub cmbVersion_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbVersion.SelectedIndexChanged

    End Sub
End Class

Thank you
 
Here's an example using a List(Of String) and inserting your string at line 21 (index 20).

VB.NET:
		Dim hostsFile As String = String.Format("{0}\drivers\etc\hosts", Environment.SystemDirectory)
		Dim hostsContents As New List(Of String)

		Using sr As New IO.StreamReader(hostsFile)
			hostsContents.AddRange(sr.ReadToEnd.Split(Environment.NewLine))
		End Using

		hostsContents.Insert(20, "127.0.0.1 www.007guard.com")

		Using sw As New IO.StreamWriter(hostsFile)
			sw.Write(String.Join(Environment.NewLine, hostsContents.ToArray))
		End Using
 
Thank you for the quick reply. Please forigve my stupidity i have only been using vb.net for a few weeks. This is only just some fun for me to learn

I inserted your code for one of my selections. I noticed quite a few things where highlighted blue underneath wrong.

Do i have to do something else to get this to work with my code?

sorry for being stupid
 
Johnson (VS 2003) said:
i have only been using vb.net for a few weeks...
Do i have to do something else to get this to work with my code?
Then you should have no problems start using the current VB version for free and not a 6-7 years old costly one. ;)
 
ah thats much better. Nice version.

I get the error type 'list' is not defined

List(Of String)

thats highlighted
 
System.Collections.Generic namespace is imported by default in all projects from VB 2005 and up. May you have removed that import, or perhaps more likely upgrading a VB 2003 project doesn't add it? Have a look in the References page in project properties, the global import for this namespace should be ticked. You can also start a new project and check same page to what references and imports are part of a default VB 2008 project.
 
It's not there?

b6o843.png
 
Yes, the namespace is in the Namespaces list below, either ticked or not.
 
Also i use bg music. i added the wav file to the project. but not sure how to play it at start up?
 
Last edited:
You fellas are brillient. :D

Just one alst thing.

After it has written 127.0.0.1 www.thisisatest.com - This Is A Test.com it hads a space below it. How can i stop this

Sorry, I didn't check to see you were using .NET 1.1 on my original reply.

If you're getting a space then your original file had a space in it. You can loop through your list and remove the items from the collection that are an empty string before using the StreamWriter to write the file.

For playing a wav file try this link: Visual Basic Express - Learn. Lesson 1 should have what you're looking for.

In the future when you change topics like this you should create an additional thread so that other members know of your new question. If a passing mod can split this that would be great :).
 
Back
Top