0

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 .

  • Too many `End If` and `Next`. Missing pieces of code? And did you get any error message? – Steve Jul 07 '13 at 12:35
  • just 1 warning ...: Variable 'result' is used before it has been assigned a value. A null reference exception could result at runtime. – Andy Gherghina Jul 07 '13 at 12:51
  • and I m sure that nothing is missing . Just my bad understanding of commands – Andy Gherghina Jul 07 '13 at 12:52
  • sorry .. you were right... i had too many end if and next... now it should look right – Andy Gherghina Jul 07 '13 at 12:58
  • If the directory contains more than one file with name that starts with "Server.?????" your loop ends setting the variable result to a file name that could not be the one you hope to write. Check if your directory contains just the "server.properties" file that you are searching for. – Steve Jul 07 '13 at 13:03
  • yea.. .there is only one server with extension .properties in this folder – Andy Gherghina Jul 07 '13 at 14:00
  • i get no errors.. but the text is not even touched... is just empty – Andy Gherghina Jul 07 '13 at 14:01

1 Answers1

0

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.

Steve
  • 213,761
  • 22
  • 232
  • 286