Hi i have a vb project im getting a bit of trouble with.

this is how it should work

user input text in the textbox.
the input text will be count as single character each one by one
after been checked they will be writen to a text file but all in caps
if use input symbols, there is a look up table to turn that symbol in charater and then write it to the text file.
after all that are writen to the text file another part of the program will copy the text file to another location and delete the original one.
so here is my problem because the text file will be deleted i need a code that will create a text file each time the programme runs.

in my code i use

Dim myfile As StreamWriter
myfile = File.CreateText("path of the file\thetextfile.txt")
and after this line i have the codes to convert character to all caps and also symbol to insert into the text file

then i have the code to write the data to the text file

Dim objFileToWrite As StreamWriter
objFileToWrite = CreateObject("Scipting.FileSystemObject").OpenTextFile("path of the file\thetextfile.txt", 2, True)
objFileToWrite.WriteLine(strmessage)
objFileToWrite.Close()
each time i run the aplication when it reach the part to write to the text file the programme freezes or it return an error cannot write to file beacause it is in use by another program.

can anybody help me

Dani AI

Generated

The freeze/error comes from the file being opened more than once and held open. is on the right track — once a StreamWriter (or any writer) owns the file handle, a second open attempt will fail with "file in use". Mixing System.IO and the COM Scripting.FileSystemObject only makes this more likely. Pick one API and make sure every stream is closed or disposed before you try to open the same path again.

A robust fix is to open the file once, write everything, then let the runtime close it automatically. The Using pattern guarantees disposal even on exceptions. Example pattern (replace MapSymbol with your lookup logic):

Dim path As String = "C:\path\thetextfile.txt"
Using w As New System.IO.StreamWriter(path, False)
    For Each ch As Char In inputText
        Dim outCh = MapSymbol(Char.ToUpperInvariant(ch))
        w.Write(outCh)
    Next
End Using

If you prefer higher-level helpers, build the final string and call System.IO.File.WriteAllText or File.AppendAllText to avoid managing streams. To avoid the "file already exists / lock" problem when you create a fresh file each run, give it a unique name (timestamp or GUID):

Dim fileName = System.IO.Path.Combine(folder, "thetextfile_" & DateTime.Now.ToString("yyyyMMdd_HHmmss") & ".txt")
System.IO.File.WriteAllText(fileName, finalText)

Other practical tips: always handle IOExceptions and log them, confirm the app has write permission on the folder, and don’t forget antivirus or an editor may briefly lock the file. If another process must read while you write, open the FileStream with an appropriate FileShare (for reads) and write via a StreamWriter over that stream. For , start by removing the-second-open (or add a Close/Using) and test again — that single change usually resolves the freeze.

Hi,

You have already opened the file with variable "myfile", before closing that,
you cannot again open the same file...
You have to close that file and re-open... or open one more new file with diff name for writing
write all the data.. close and rename it with original path.

Regards
Veena

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.