furjaw

I am trying to add a feature to my application where it automatically performs a backup everytime that the user exits the program. I added the following as the last statement in the FormClosing routine:

Process.start("Backup.exe")

I am getting an error message that states that it cannot copy a file because it is in use.

How can I have the application release all files before executing the Process.start



Re: Visual Basic Express Edition How can I have my application close all files?

Omnicoder

You cant(unless your program is the one it is open with), but you can check wheter or not it is ready before copying it(unfortunalitly, im really tired right now and i can thnik of the codde of the top of my head




Re: Visual Basic Express Edition How can I have my application close all files?

furjaw

The program that needs to release all files is the one that opened them.

I want to release all resources before doing the Process.Start("Backup.exe").





Re: Visual Basic Express Edition How can I have my application close all files?

nobugz

You'll have to close all your open files. If that's a problem, it shouldn't be, you could start your program from a .bat file that starts your program first, then runs Backup. Look into the "Using" statement.





Re: Visual Basic Express Edition How can I have my application close all files?

furjaw

I tried the batch file idea. My user complained about the black screens that did not go away.





Re: Visual Basic Express Edition How can I have my application close all files?

Omnicoder

thats the cmd window




Re: Visual Basic Express Edition How can I have my application close all files?

WayneSpangler

How about a program that runs your program. i.e.

Process.Start("your.exe")

Process.Start("backup.exe")






Re: Visual Basic Express Edition How can I have my application close all files?

furjaw

That wouldn't work because your.exe would have the files open and backup.exe needs exclusive use.

The answer is .ReleaseAllResources, but, I can't figure out how to invoke it.

MSDN gives all kinds of cryptic information that is useless when the only thing that helps is a code sample.





Re: Visual Basic Express Edition How can I have my application close all files?

nobugz

That's for resources like bitmaps etc. Files are not resources. There are few reasons why your program should have opened files while it is idle. You probably are forgetting to call Close() on a stream, that is easy to do and hard to notice. You should have an idea what stream you are forgetting to Close() from the name of the file that your backup program is complaining about.

Again, use the Using keyword to ensure streams are closed and disposed, even when there are exceptions. Typical usage is:

Using fs As New StreamWriter("c:\temp\test.txt", False)
fs.WriteLine("nobugz waz here")
End Using

With Using, you don't have to call Close() nor Dispose(). Recommended.







Re: Visual Basic Express Edition How can I have my application close all files?

furjaw

I am getting "The process cannot access the file 'C:\Program Files\OrthoLabRx\AcrylicColors.mdf' because it is being used by another process."





Re: Visual Basic Express Edition How can I have my application close all files?

nobugz

Sounds like a SQL Express database. Not sure how you'd tell it to stop using an .mdf. Post at a SQL forum...





Re: Visual Basic Express Edition How can I have my application close all files?

furjaw

It's working good now.

I just added a try, catch so, it just waits until the calling program ends releasing the files.

For Each foundFile As String In fileList
Dim succeeded = False
Do Until succeeded = True
Try
My.Computer.FileSystem.CopyFile(foundFile, BackupDirectory & "\" & foundFileName, True)
succeeded = True
Catch
End Try
Loop
Next
TextBox2.Text = "Backup to " & Drive & "\OrthoLabRx Backup complete"