开发者

Accept parameters from a .Net console app into a Batch file

开发者 https://www.devze.com 2023-04-12 19:48 出处:网络
I am trying to pass parameters from a .Net console app to 开发者_高级运维a batch file. The parameters are not coming into the batch file.

I am trying to pass parameters from a .Net console app to 开发者_高级运维a batch file. The parameters are not coming into the batch file.

How can I properly set up passing the parameters into the bat file?

Here is the method in the console app that I'm executing.

private static int ProcessBatFile(string ifldr, string ofldr, string iext, string oext, Int16 filewidth, Int16 fileheight, Int16 ctr)
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = ConfigurationSettings.AppSettings.Get("BatProcessDir") + "imagemagick.bat";
            psi.Arguments = "-ifldr=" + ifldr + " -ofldr=" + ofldr + " -iext=" + iext + " -oext=" + oext + " -iwid=" + filewidth + " -ihgt=" + fileheight;
            psi.UseShellExecute = false;

            Process process = new Process();
            process.StartInfo = psi;
            process.Start();

            return ctr;
        }

Below, is the code in the bat file I'm trying to execute:

@echo on

echo %ofldr%

echo %ifldr%

echo %iwid%

echo %ihgt% 

echo %oext% 

echo %iext%


If you pass them as paramters, you can do this in the c# code:

psi.Arguments = ifldr + " " + ofldr + " " + iext + " " + oext + " " + filewidth + " " + fileheight;

and do this in the batch file:

@echo on
set ifldr=%1
set ofldr=%2
set iext=%3
set oext=%4
set iwid=%5
set ihgt=%6

echo %ofldr%
echo %ifldr%
echo %iwid%
echo %ihgt% 
echo %oext% 
echo %iext%

As an alternative solution, you can also directly modify the environment before executing the batch file using System.Environment.SetEnvironmentVariable:

System.Environment.SetEnvironmentVariable ("ifldr", ifldr);
....

This causes less problems if the parameters may contain spaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消