Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Recommended Posts

Hi All,

I know there are a lot of vb programmers here so I thought i'd ask. I have a program that checks if a file exists. If not it creates it.

 

As long as I do not use the routine which creates the file all is good.

If the file does not exist the routine crashes at the streamwriter routine with the error: File is in use by another process. The snippet is here :

 

If System.IO.File.Exists(strFileName) = False Then

' Create File

Dim file As System.IO.FileStream

file = System.IO.File.Create(strFileName)

 

End If

 

' File exist already

Dim objWriter As New System.IO.StreamWriter(strFileName)

' Write Header

objWriter.WriteLine("Job# " & txtJobNumber.Text)

 

Do I need to close the file after I create it? If so how?

Thanks in advance!!!

Karl

Link to comment
Share on other sites

Assuming you want to delete an existing file you could try something like the code below. Note that I didnt test this but it should work.

 

code:

   Dim objWriter As System.IO.StreamWriter = Nothing

 

Try

 

If System.IO.File.Exists(strFileName) Then

' If it exists do we delete it?

System.IO.File.Delete(strFileName)

End If

 

' Create a new file

objWriter = New System.IO.StreamWriter(strFileName)

' Write Header

objWriter.WriteLine("Job# " & txtJobNumber.Text)

 

 

 

Catch ex As Exception

 

 

Finally

 

If Not objWriter Is Nothing Then

objWriter.Close()

End If

 

 

 

End Try

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...