Avoid file deletion
-
How can i prevent user from deleting a file when the file is under use by another process ? I have an application which creates a directory called (say) "Test" and creates a default "Text.xml" file in it.I dont want user to delete the "Test.xml" when the application is in use. what is the code for it. Currently ,user can delete the file when application is open. Please tell me the code for it...
-
How can i prevent user from deleting a file when the file is under use by another process ? I have an application which creates a directory called (say) "Test" and creates a default "Text.xml" file in it.I dont want user to delete the "Test.xml" when the application is in use. what is the code for it. Currently ,user can delete the file when application is open. Please tell me the code for it...
You can't delete an open file (well you might be able to if you fiddle around with permissions enough) so just keep the file open for the duration of your app. This code:
[STAThread]
static void Main(string[] args)
{
FileStream fs = File.Open(@"C:\bobbins.txt", FileMode.Open);
Console.ReadLine();
fs.Close();
}Prevents me from deleting the file bobbins.txt until my console app closes.
Regards, Rob Philpott.
-
How can i prevent user from deleting a file when the file is under use by another process ? I have an application which creates a directory called (say) "Test" and creates a default "Text.xml" file in it.I dont want user to delete the "Test.xml" when the application is in use. what is the code for it. Currently ,user can delete the file when application is open. Please tell me the code for it...
snoby wrote:
How can i prevent user from deleting a file when the file is under use by another process ?
The file cannot be deleted while an application is using it. What is ... well part of Windows. Run an application and try to delete it ... you cant. Other wise if I am not understanding you correctly, you can open the file for reading and only after the appliation has finished with the file, you can close the file. something like ...
// this is just a concept ...
form onload()
open file for reading
form_onclose()
close fileHope it helps ... Regards,
The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^
-
You can't delete an open file (well you might be able to if you fiddle around with permissions enough) so just keep the file open for the duration of your app. This code:
[STAThread]
static void Main(string[] args)
{
FileStream fs = File.Open(@"C:\bobbins.txt", FileMode.Open);
Console.ReadLine();
fs.Close();
}Prevents me from deleting the file bobbins.txt until my console app closes.
Regards, Rob Philpott.
Also I think there is a
lock
method for FileStream. Don't know it's efficiency."Legacy code" often differs from its suggested alternative by actually working and scaling. —Bjarne Stroustrup
modified on Friday, March 14, 2008 7:10 AM
-
Also I think there is a
lock
method for FileStream. Don't know it's efficiency."Legacy code" often differs from its suggested alternative by actually working and scaling. —Bjarne Stroustrup
modified on Friday, March 14, 2008 7:10 AM
Ah, not sure about that; never used it, but if so that sounds better!
Regards, Rob Philpott.
-
snoby wrote:
How can i prevent user from deleting a file when the file is under use by another process ?
The file cannot be deleted while an application is using it. What is ... well part of Windows. Run an application and try to delete it ... you cant. Other wise if I am not understanding you correctly, you can open the file for reading and only after the appliation has finished with the file, you can close the file. something like ...
// this is just a concept ...
form onload()
open file for reading
form_onclose()
close fileHope it helps ... Regards,
The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^
What if want to be sure that no data is lost? Keeping a file opened could cause all data is lost on crush or immediate shut down. I suppose that such construct could be used:
open file;
// ...
write data which shouldn't get lost;
close the file to save it; (*)
reopen it;
// ...
and so on.But in could theoretically happen that user opens a file just after (*), before it's opened in the next command. The propability of this is near 0, anyway what to do then?
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
What if want to be sure that no data is lost? Keeping a file opened could cause all data is lost on crush or immediate shut down. I suppose that such construct could be used:
open file;
// ...
write data which shouldn't get lost;
close the file to save it; (*)
reopen it;
// ...
and so on.But in could theoretically happen that user opens a file just after (*), before it's opened in the next command. The propability of this is near 0, anyway what to do then?
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
Just holding the file open won't do that. It's when you write data to the file without flushing it, or shut the machine off in the middle of a disk write that you run into this problem.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007