because it is being used by another process
-
In my program I create a file using: dim strFileName as string = "c:\test.txt" File.Create(My.Settings.strFileName) I'm just using the file as a basic reference that the program is running and while this file exists don't try and start another instance of the program. I don't use the file or add to it or anything. Later I try to delete the file using: File.Delete(My.Settings.strFileName) I've also tried Kill(My.Settings.strFileName) Unfortunately I get this error: The process cannot access the file 'c:\test.txt' because it is being used by another process. I understand the error, but how do I get the program to let go after I create it so I can delete it later? :confused:
Lost in the vast sea of .NET
-
In my program I create a file using: dim strFileName as string = "c:\test.txt" File.Create(My.Settings.strFileName) I'm just using the file as a basic reference that the program is running and while this file exists don't try and start another instance of the program. I don't use the file or add to it or anything. Later I try to delete the file using: File.Delete(My.Settings.strFileName) I've also tried Kill(My.Settings.strFileName) Unfortunately I get this error: The process cannot access the file 'c:\test.txt' because it is being used by another process. I understand the error, but how do I get the program to let go after I create it so I can delete it later? :confused:
Lost in the vast sea of .NET
You can accomplish the same goal by going to the Project settings, view the Application Tab, and make sure the checkbox that says Make single instance application is checked. Now only one instance of your application can run at a time. Also, if you want to, you can View the Application Events and add and event called the MyApplication_StartupNextInstance. You can add code here to perform a specific task if a user tries to run the application more than once. Hope this helps.
-
In my program I create a file using: dim strFileName as string = "c:\test.txt" File.Create(My.Settings.strFileName) I'm just using the file as a basic reference that the program is running and while this file exists don't try and start another instance of the program. I don't use the file or add to it or anything. Later I try to delete the file using: File.Delete(My.Settings.strFileName) I've also tried Kill(My.Settings.strFileName) Unfortunately I get this error: The process cannot access the file 'c:\test.txt' because it is being used by another process. I understand the error, but how do I get the program to let go after I create it so I can delete it later? :confused:
Lost in the vast sea of .NET
File.Create
does not just create a file, it also opens it and returns a reference to the createdFileStream
. Try this:Dim MyFile As FileStream = File.Create(My.Settings.strFileName)
You can then use
MyFile
to add text. When you are done, callMyFile.Close
File.Delete(My.Settings.strFileName) -
File.Create
does not just create a file, it also opens it and returns a reference to the createdFileStream
. Try this:Dim MyFile As FileStream = File.Create(My.Settings.strFileName)
You can then use
MyFile
to add text. When you are done, callMyFile.Close
File.Delete(My.Settings.strFileName)Thanks!!! That worked perfectly!
Lost in the vast sea of .NET
-
You can accomplish the same goal by going to the Project settings, view the Application Tab, and make sure the checkbox that says Make single instance application is checked. Now only one instance of your application can run at a time. Also, if you want to, you can View the Application Events and add and event called the MyApplication_StartupNextInstance. You can add code here to perform a specific task if a user tries to run the application more than once. Hope this helps.
My app is being called by a service and isn't really Form based at all. I currently don't have the application framework enabled. This suggestion was great... I didn't know this feature existed, but I'll try it another day on a different app which is more based around forms. Thanks!! :)
Lost in the vast sea of .NET
-
Thanks!!! That worked perfectly!
Lost in the vast sea of .NET
If you're using a file to prevent a second instance from being opened, you have a problem. This won't work in all cases since you can have two instances of your app both checking for the existance of the file and both trying to create the file at the same time. A better solution is to create a Mutex instead. A file can be created by multiple processes at the same time. A Mutex cannot. If two processes try to create the same mutex as the same time, one of them is forced to wait until the other request completes. Documention on the Mutex class can be found here[^]. Just be sure to read it and to release the Mutex when your app is done with it.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...modified on Tuesday, April 6, 2010 6:44 PM
-
If you're using a file to prevent a second instance from being opened, you have a problem. This won't work in all cases since you can have two instances of your app both checking for the existance of the file and both trying to create the file at the same time. A better solution is to create a Mutex instead. A file can be created by multiple processes at the same time. A Mutex cannot. If two processes try to create the same mutex as the same time, one of them is forced to wait until the other request completes. Documention on the Mutex class can be found here[^]. Just be sure to read it and to release the Mutex when your app is done with it.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...modified on Tuesday, April 6, 2010 6:44 PM
Thanks! That does sound like a better solution! I'll look into it. :)
Lost in the vast sea of .NET