重定向命令行参数以进行引导
我正在尝试用 C 而不是 C# 重写以下程序(它的可移植性较差)。显然,“int system (const char * command)”对于完成该程序是必要的。以“int main ( int argc, char * argv[] )”开头将允许获取命令行参数,但仍然存在一个难以理解的问题。如何成功地转义带有空格的参数?在下面的程序中,带有空格的参数(例如:screensaver.scr“这是一个测试”)将作为单独的参数传递给脚本(例如:screensaver.scr 这是一个测试),并且很容易导致问题。
namespace Boids_Screensaver
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
System.Diagnostics.Process python = new System.Diagnostics.Process();
python.EnableRaisingEvents = false;
python.StartInfo.FileName = "C:\\Python31\\pythonw.exe";
python.StartInfo.Arguments = "boids.pyw";
foreach (string arg in args)
{
python.StartInfo.Arguments += " " + arg;
}
python.Start();
}
}
}
I am trying to rewrite the following program in C instead of C# (which is less portable). It is obvious that "int system ( const char * command )" will be necessary to complete the program. Starting it with "int main ( int argc, char * argv[] )" will allow getting the command-line arguments, but there is still a problem that is difficult to understand. How do you successfully escape arguments with spaces in them? In the program below, arguments with spaces in them (example: screensaver.scr "this is a test") will be passed to the script as separate arguments (example: screensaver.scr this is a test) and could easily cause problems.
namespace Boids_Screensaver
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
System.Diagnostics.Process python = new System.Diagnostics.Process();
python.EnableRaisingEvents = false;
python.StartInfo.FileName = "C:\\Python31\\pythonw.exe";
python.StartInfo.Arguments = "boids.pyw";
foreach (string arg in args)
{
python.StartInfo.Arguments += " " + arg;
}
python.Start();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Windows 都乱了。每个程序都有自己的规则。
Windows is all messed up. Every program has its own rules.
在 Windows 下执行此操作的正确方法是使用 _spawnv< /a>
在类似 UNIX 的操作系统下,其等效项是
fork()
后跟execv
。The correct way to do this under windows is to use _spawnv
Its equivalent under unix like OSes is
fork()
followed byexecv
.对不起我的英语:)。
Sorry my English :).