9
votes

Essayer de créer un fichier et de la supprimer immédiatement

// (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?

0 commentaires

4 Réponses :


23
votes

fichier.create code> remonte à un flux, que vous n'avez pas fermé.

using(File.Create(path)) {}
File.Delete(path);


0 commentaires

4
votes

Lorsque vous avez créé le fichier, vous l'utilisez jusqu'à ce que vous le fermiez - vous ne l'avez pas fait, d'où l'erreur.

Pour fermer le fichier, vous devez envelopper la création dans un en utilisant Code> Déclaration: P>

using(var file = File.Create(Path.Combine(folder, "testfile.empty")))
{
}
File.Delete(Path.Combine(folder, "testfile.empty"));


0 commentaires

1
votes

Essayez ..

File.Create(Path.Combine(folder, "testfile.empty")).Dispose();
File.Delete(Path.Combine(folder, "testfile.empty"));


0 commentaires

1
votes

Créer La méthode renvoie un filtream qui doit être proche avant de faire d'autres opérations: xxx


0 commentaires