tags:

views:

84

answers:

3

I have a c# program through which i am opening cmd window as a a process. in this command window i am running a batch file. i am redirecting the output of that batch file commands to a Text File. When i run my application everything seems to be ok.

But few times, Application is giving some error like "Can't access the file. it's being used by another application" at the same time cmd window is not getting closed. If we close the cmd process through the Task Manager, then it's writing the content to the file and getting closed. Even though i closed the cmd process, still file handle is not getting released. so that i am not able to run the application next time onwards.Always it's saying Can't access the file. Only after restarting the system, it's working.

Here is my code:

Process objProcess = new Process();
ProcessStartInfo objProInfo = new ProcessStartInfo();
objProInfo.WindowStyle = ProcessWindowStyle.Maximized;
objProInfo.UseShellExecute = true;
objProInfo.FileName = "Batch file path"                
objProInfo.Arguments = "Some Arguments";
if (Directory.Exists(strOutputPath) == false)
{
  Directory.CreateDirectory(strOutputPath);
}               
objProInfo.CreateNoWindow = false;
objProcess.StartInfo = objProInfo;                
objProcess.Start();                
objProcess.WaitForExit();

test.bat:

 java classname argument > output.txt

Here is my question:

  1. I am not able to trace where the problem is..

  2. How we can see the process which holding handle on ant file.

  3. Is there any suggestions for Java and .net interoperability

A: 

You can try forking and attaching file descriptor from C# rather than launching a bat file.

arunmur
why to spawn the new process from parent process. AS fork will creates child process similar to Parent, i can't use the parent process to execute batch file.Because Here my parent is an excel application. Please correct me if i am wrong....
Dinesh
A: 

I think the problem is because the java program is accessing the text file when the C# program is writing something on it, and hence a "file cannot access" problem.

If I were you, I would do everything in C#-- I won't use Java to read the state of the C# program. And I would access the file only after I've completed whatever the C# needs to do.

As for to see what process is locking up your file, you can use Process Explorer for this purpose.

Ngu Soon Hui
It is more like, Java will be able to access source system, but can't be used for Client development. C# can be used for Client development but can't be used to access the source system. Thats why i am using this approach.
Dinesh
@dineshrekula, C# *can* be used to access the source system-- if you are developing on MS platform, I suggest you use C# exclusively
Ngu Soon Hui
It's not possible to access source system. My Source system is Team Center.It's a PLM tool.
Dinesh
+1  A: 

In situations like this I start up the Process Explorer ( by Sysinternals, awesome tool btw ) click Ctrl+F, and enter the name of the file. It will search across all running processes and will list the file handles to this file by the applications that have it open.

You can then either drop the handle, or kill the app - whatever you think is better )

Artiom Chilaru
Thanks a lot... Even though it didn't solved my problem directly, it helped me a lot in identifying the root cause.
Dinesh
so what was the problem then? I'm intrigued :)
Artiom Chilaru