I am trying to execute a batch file which directs 7zip to compress a directory. The batch file is working fine when I run it by 'double-clicking' the file or if I try to run it in the command Prompt. But I am having problem while I am try to execute the file through a C# application. Below is my code in C#.
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = path.Substring(0, path.IndexOf("Debug") + 6) + "Scripts";
String EnvironmentPath = System.Environment
.GetEnvironmentVariable("path",
EnvironmentVariableTarget.Machine);
string[] varList = EnvironmentPath.Split(';');
string enviVar= varList.First(x=>x.Contains("7-Zip"));
Process proc = new Process();
proc = new Process();
proc.StartInfo.WorkingDirectory = path;
proc.StartInfo.Arguments = enviVar;
proc.StartInfo.FileName = "Script_To_BackUp_DB.bat";
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
The value in path variable is : D:\Projects\Common\common\Common\Utilities\Utilities\bin\Debug\Scripts. If I remove the "proc.StartInfo.UseShellExecute = false" line, then batch will execute and close with an exception stating 7z is not recognized as Internal or external command. And I have already set the path in Environment Variable.
The batch file code is:
set backup_dir=C:\Users\FU386DKH\Desktop\Card logs\
set db_dir=D:\Projects\Projects\db\
:: set dt string in dd_mm_yy_HH_MM_SS format
set dt=%Date:~0,2%_%Date:~3,2%_%Date:~6,4%_%Date:~0,2%_%Date:~3,2%_%Date:~6,2%
:: compress folder
7z a -tzip "%backup_dir%_%dt%.zip" "%db_dir%" -ssw