将 .Net 控制台应用程序中的参数接受到批处理文件中

发布于 2024-12-09 10:14:21 字数 945 浏览 1 评论 0原文

我正在尝试将参数从 .Net 控制台应用程序传递到批处理文件。参数不会进入批处理文件。

如何正确设置将参数传递到bat文件中?

这是我正在执行的控制台应用程序中的方法。

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;
        }

下面是我尝试执行的bat文件中的代码:

@echo on

echo %ofldr%

echo %ifldr%

echo %iwid%

echo %ihgt% 

echo %oext% 

echo %iext%

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%

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风追烟花雨 2024-12-16 10:14:21

如果将它们作为参数传递,则可以在 C# 代码中执行此操作:

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

并在批处理文件中执行此操作:

@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%

作为替代解决方案,您还可以在执行批处理文件之前使用 System.Environment.SetEnvironmentVariable 直接修改环境:

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

如果参数可能包含空格,这会导致较少的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文