This is "part" of my code

Imports System
Imports System.IO
Imports System.Text

Public Class Form2

    Public fo1 As String

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim path As String = Directory.GetCurrentDirectory() & "\test.txt"
        If File.Exists(path) Then
            My.Computer.FileSystem.WriteAllText(path, fo1, True)
        Else
           Dim fs As FileStream = File.Create(path)
        End If
    End Sub
End Class

fo1 is this : fo1 = OpenFileDialog.FileName()

No errors but not writeing to file

if i replace fo1 with any text "test text" it works

I am noob in vb just started useing

Recommended Answers

All 11 Replies

Your code is sound. If fo1 = "" then nothing will be written to the file, otherwise the value of fo1 will be written to the file. On a side note the My.Computer.FileSystem.WriteAllText will create a file that doesn't exist automatically, no need to create one explicitly.

Perhaps you haven't put any text into fo1. In any case you don't need to have the If. We know the current directory exists and WriteAllText will create the file if it doesn't exist.

tinstaafl so what should i use instead of My.Computer.FileSystem.WriteAllText ?

Reverend Jim fo1 always have text it's set to the path of the file i select via OpenFileDialog

if i do TextBox1.Text = fo1 it sets the text in the text box to fo1

ok...i just tryed something

        Dim path As String = Directory.GetCurrentDirectory() & "\Setings.txt"
        If File.Exists(path) Then
            Dim test1 As New System.IO.StreamWriter(path, False)
            test1.WriteLine(fo1)
            test1.Close()
            MsgBox(fo1)
        Else
            Dim fs As FileStream = File.Create(path)
        End If

i added the MsgBox(fo1) to see if fo1 is set
it's not set :( it is null

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim fo1 As String
        OpenFileDialog1.Filter = "Executabiles|*.exe| All|*.*"
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            fo1 = OpenFileDialog1.FileName()
            TextBox1.Text = fo1
        End If
    End Sub

it sets the TextBox1 but doesent get out from the private sub

make fo1 global. Declare it inside the main form class but before any procedures. This bit of code from your original questions should explain it.

Imports System.Net
Imports System.IO

Public Class Form1
    Dim Fo1 As String = ""
    Sub test1()

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim path As String = Directory.GetCurrentDirectory() & "\test.txt"
            My.Computer.FileSystem.WriteAllText(path, fo1, True)
    End Sub
End Class

Also, if you are simply wanting to copy one file from one location to another, you can use this call:

    Dim outFileName As String = "myTXT.txt"
    Dim input As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\myTXT.txt"
    Dim output As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\TEST\" & outFileName

    If IO.File.Exists(input) Then
        If IO.Directory.Exists(output.TrimEnd.TrimEnd(outFileName.ToCharArray)) = False Then
            If MsgBox("Directory does not exist:" & vbCrLf & output.TrimEnd(outFileName.ToCharArray) & vbCrLf & "Do you want to create it?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                IO.Directory.CreateDirectory(output.TrimEnd(outFileName.ToCharArray))
                IO.File.Copy(input, output)
            End If
        Else
            IO.File.Copy(input, output)
        End If
    Else
        MsgBox("Could not find file specified:" & vbCrLf & input & vbCrLf & "Process aborted!")
        Exit Sub
    End If

This will make an exact copy.

Your code

Dim path As String = Directory.GetCurrentDirectory() & "\Setings.txt"
If File.Exists(path) Then
    Dim test1 As New System.IO.StreamWriter(path, False)
    test1.WriteLine(fo1)
    test1.Close()
    MsgBox(fo1)
Else
    Dim fs As FileStream = File.Create(path)
End If

will not write anything to the file if the file does not exist. Look at it in pseudo code

compose file name

if the file exists then
    write the contents of fo1 to the file
otherwise
    create the file but don't write anything to it

As I mentioned before, the WriteAllText will create the file if it doesn't exist so you don't need the test. The following seems to be what you intended

Imports System.IO

Public Class Form1

    Public fo1 As String = "some test text"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim filename As String = Directory.GetCurrentDirectory() & "\test.txt"
        MsgBox("writing " & fo1 & " to " & filename)
        My.Computer.FileSystem.WriteAllText(filename, fo1, True)

    End Sub

End Class

Reverend Jim if i set

Public fo1 As String = "Some text"

then fo1 = "Some text" and it writes to file "Some text"

but i need fo1 to be :

fo1 = OpenFileDiablo1.FileName()

as in my script

     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim fo1 As String
    OpenFileDialog1.Filter = "Executabiles|*.exe| All|*.*"
    If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    fo1 = OpenFileDialog1.FileName()
    TextBox1.Text = fo1
    End If
    End Sub

Take line 2 out of your code. If you look at the code we both put up, you only create fo1 at the front of the form1 class, but you can access and change it anywhere with the form1 class.
Try this:

Imports System.Net
Imports System.IO
Public Class Form1
    Dim Fo1 As String = ""
    Sub test1()
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        OpenFileDialog1.Filter = "Executabiles|*.exe| All|*.*"
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            fo1 = OpenFileDialog1.FileName()
            TextBox1.Text = fo1
        End If
    End Sub
End Class

I only put "some text" into the string for testing and to demonstrate that the text is still there inside the event handler. You can assign it any text you want at any point in your code. As tinstaafl points out, if you declare fo1 inside the handler it overrides the global copy with a local one and makes the global fo1 inaccessible.

got it :)
10x for all your help and patience

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.