The process cannot access the file because it is being used by another process.

When I run the console .net application from the bat file, for example. start myapp.exe

myapp.exe then tries to write the file to its current directory, although I get a .net error stating that the file is being used by another application (nothing else works)

http://i.stack.imgur.com/XLmnR.png

Although I run it, usually without a batch file, for example. double click on it, it works fine and displays the file in order. I thought this might be due to privileges, although I tried to run the batch file as an administrator, and I got the same error "The file is in use ..."

Can anyone shed some light on this?

code:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace FileSearcher { class Program { static void Main(string[] args) { string dirPath = "C:\\"; FileSystemWatcher fileWatcher = new FileSystemWatcher(dirPath); fileWatcher.IncludeSubdirectories = true; fileWatcher.Filter = "*.exe"; // fileWatcher.Filter = "C:\\$Recycle.Bin"; // fileWatcher.Changed += new FileSystemEventHandler(FileWatcher_Changed); fileWatcher.Created += new FileSystemEventHandler(FileWatcher_Created); // fileWatcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted); // fileWatcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed); fileWatcher.EnableRaisingEvents = true; // updated code Console.ReadKey(); } static void FileWatcher_Renamed(object sender, RenamedEventArgs e) { Console.WriteLine(e.OldName + " was renamed to " + e.Name); } static void FileWatcher_Deleted(object sender, FileSystemEventArgs e) { Console.WriteLine(e.Name + " was deleted"); } static void FileWatcher_Created(object sender, FileSystemEventArgs e) { using (StreamWriter fileWriter = new StreamWriter("process.lst", true)) { var data = true; fileWriter.Write("C:\\" + e.Name + Environment.NewLine); } } static void FileWatcher_Changed(object sender, FileSystemEventArgs e) { Console.WriteLine(e.Name + ""); } } } 
+4
source share
3 answers

It looks like from your batch file you are sending stdout (1>) to the same file (process.lst) that you are writing in your application. You can do one or the other, not both .

For example, this application works fine at startup:

 static void Main(string[] args) { StreamWriter writer = File.CreateText("process.lst"); Console.WriteLine("Writing to the file."); writer.Write("Testing 1.2.3.4"); Console.WriteLine("Finished."); } 

But when launched from the command line, for example myTestApp.exe 1> process.lst , the same exception appears as yours:

The process cannot access the file 'process.lst' because it is being used by another process.

+2
source

Try your process after the OnCreated event completes. Perhaps starting a short timer and recording a file when the timer is ticking (remember to stop the timer)

0
source

The event in which the file was created is fired immediately, even if the file is not already written. You should always try to open the file and wait a bit when you get an IOException. You can find the solution here: http://bloggingabout.net/blogs/jschreuder/archive/2006/07/06/12886.aspx

0
source

All Articles