// (1) create test file and delete it again
File.Create(Path.Combine(folder, "testfile.empty"));
File.Delete(Path.Combine(folder, "testfile.empty"));
The last line throws an exception:
The process cannot access the file '\\MYPC\C$_AS\RSC\testfile.empty' because it is being used by another process.
Why is that?
Setting a file to auto-delete button for the file and select More Actions>Set Expiration. Check off the box to Auto-delete this item on a selected date and use the box to select the appropriate date for deletion. Click Save to save your changes.
In most cases, the reason you cannot delete a file is quite simple. The file or a file from the folder is still open or being used by another program.
Press Shift + Delete to force delete a file or folder If the problem is due to the Recycle Bin, you can select the target file for folder, and press Shift + Delete keyboard shortcut to permanently delete it.
One is simply using the delete option, and the other one is deleting files permanently. When you can't delete a file normally, you can delete undeletable files Windows 10 by selecting the target file or folder and then press Shift + Delete keys on the keyboard for a try.
File.Create
hands you back a stream, that you haven't closed.
using(var file = File.Create(path)) {
// do something with it
}
File.Delete(path);
should work; or easier:
File.WriteAllBytes(path, new byte[0]);
File.Delete(path);
or even just:
using(File.Create(path)) {}
File.Delete(path);
When you created the file, you are using it until you close it - you have not done so, hence the error.
In order to close the file, you should be wrapping the creation in a using
statement:
using(var file = File.Create(Path.Combine(folder, "testfile.empty")))
{
}
File.Delete(Path.Combine(folder, "testfile.empty"));
try ..
File.Create(Path.Combine(folder, "testfile.empty")).Dispose();
File.Delete(Path.Combine(folder, "testfile.empty"));
Create
method returns a filestream which must be close before making other operations:
FileStream fs=File.Create( "testfile.empty");
fs.Close();
File.Delete("testfile.empty");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With