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

this is how it should work

  1. user input text in the textbox.
  2. the input text will be count as single character each one by one
  3. after been checked they will be writen to a text file but all in caps
  4. if use input symbols, there is a look up table to turn that symbol in charater and then write it to the text file.
  5. 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

Why don't you just use:

Dim fileOut as new StreamWriter("path\filename.txt")

...then write to it, then close it and move it?

...or can you JUST write to the a file name each time without doing the double file process?
Check this example. It's a console app, but you can use the file techniques:

Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Text.RegularExpressions

Module Module1

   Function FilteredChar(ByVal chr As Char, ByVal map As Dictionary(Of Char, Char))
      If map.ContainsKey(chr) Then
         Return map(chr)
      Else
         Return chr
      End If
   End Function

   Function FilteredString(ByVal strInput As String, ByVal map As Dictionary(Of Char, Char)) As String
      Dim sb As New StringBuilder()
      strInput.ToList().ForEach(Sub(c) sb.Append(FilteredChar(c, map)))
      Return sb.ToString()
   End Function

   Function GetNewFileName(ByVal strPath As String, ByVal strSuffix As String) As String
      Dim strLastFile = Directory.GetFiles(strPath, "*." & strSuffix).LastOrDefault()
      If String.IsNullOrEmpty(strLastFile) Then strLastFile = ("000." & strSuffix)
      Dim intFileNum As Integer = Integer.Parse(Regex.Replace(Path.GetFileNameWithoutExtension(strLastFile), "[^0-9]", "")) + 1
      Return Path.Combine(strPath, intFileNum.ToString().PadLeft(3, "0") & "." & strSuffix)
   End Function

   Sub Main()
      Dim map_chr2chrSymbols = New Dictionary(Of Char, Char) From
      {
         {";", " "},
         {"#", " "},
         {"*", "."}
      }
      ''
      Dim strUserText = "This #is neat; This is Neat* ##comment"
      Dim strNewText = FilteredString(strUserText, map_chr2chrSymbols).ToUpper()
      Dim strTempPath = Path.GetTempPath()
      ''
      Dim strNewName = GetNewFileName(strTempPath, "dwf")
      File.WriteAllText(strNewName, strNewText)
   End Sub
End Module

the problem is that i have to use StreamWriter and create the file first the write to it that the requirments of the project.the thing that is weird is that if the file already exist it would write to it without any prob.but if it has to be created first then write to it give this error file is in use by another programe

You are opening two connections to the file in your code.
You only need one.

If you create a file you have to close or better dispose Create() method:

File.Create("filePath_and_fileName").Dispose()
'now you can use StreamWriter class...
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.