Reputation: 101
I am facing an issue to run the command batch file through C#. Actually I have created a batch File named Log.bat which is used to get file size of all directory files. I put this batch file in my desktop. when I run this it successfully retrieve the directories and their files size.
My script of batch file is:
dir /s > "C:\Documents and Settings\dinesh.kumar\Desktop\LogFile.txt"
This given path is my file saved location. Output:
Directory of C:\Documents and Settings\dinesh.kumar\Desktop\Color Cop
10/24/2007 12:08 AM 97,792 ColorCop.exe
08/10/2006 03:12 PM 26,235 ColorCop.HLP
08/10/2006 03:12 PM 372 file_id.diz
10/23/2007 11:50 PM 1,332 license.txt
10/24/2007 12:06 AM 10,877 readme.txt
5 File(s) 136,608 bytes
Now I have developed an windows application to run that batch file in c#. my code for run the batch file is:
protected void btnRun_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("C:\\Documents and Settings\\dinesh.kumar\\Desktop\\Log.bat");
}
But when I run this application, it creates the LogFile.txt on desktop but it give the files of microsoftvisualstudio directory like this. Output
Directory of C:\Program Files\Microsoft Visual Studio 8\Common7\IDE
08/11/2011 12:53 PM <DIR> .
08/11/2011 12:53 PM <DIR> ..
06/29/2011 04:14 PM <DIR> 1033
09/23/2005 02:53 AM 27,648 cmddef.dll
09/23/2005 04:24 AM 257,024 compluslm.dll
09/23/2005 03:37 AM 139,264 csformatui.dll
09/23/2005 07:15 AM 33,792 custsat.dll
09/23/2005 07:15 AM 1,038,848 dbghelp.dll
09/22/2005 11:16 PM 27,112 dteproperties.tlb
06/29/2011 04:14 PM <DIR> ExceptionAssistantContent
06/29/2011 04:14 PM <DIR> HTML
08/11/2011 12:53 PM 96,032 LogFile.txt
09/23/2005 06:56 AM 19,456 MakeZipExe.exe
09/23/2005 06:56 AM 393,216 Microsoft.Data.ConnectionUI.Dialog.dll
09/23/2005 06:56 AM 6,144 Microsoft.Data.ConnectionUI.dll
Sorry the data is so long. I cant paste in it.
Why is it not shows desktop directories and their files.
What did I do wrong? Any code or suggestion will be appreciated.
Thanks in advance
Upvotes: 0
Views: 274
Reputation: 1919
Change your process code which your launch to take the directory which you want (may be hardcode da path) and not the working dir as TJ said.
Upvotes: 0
Reputation: 12608
This is because your working directory is set to the application's directory when you call Process.Start.
Use the overload of Process.Start which accepts a ProccessStartInfo, then set the WorkingDirectory to the path of the batch file.
http://msdn.microsoft.com/en-us/library/0w4h05yb.aspx
Upvotes: 3