1

我正在使用 Windows 10。

我正在尝试使用 MinGW(MinGW 文件夹位于项目目录中)在 c# 中编译 c++ 文件,但它不会编译资源脚本(使用 windres)。

每当我在 cmd 中使用 windres 时,它都会显示:“C:/Users/username/AppData/Local/Temp/my.rc:1: unrecognized escape sequence”。但仍然有效。但是,当我通过 c#(通过创建进程)运行完全相同的命令时,它根本不起作用,并说:“文件名、目录名或卷标语法不正确。”。

我的代码:

String tempDir = Path.GetTempPath();
String file = tempDir + "my.rc";

using (StreamWriter writer = new StreamWriter(file, false, Encoding.ASCII))
{
     if (!textIcon.Text.Equals(""))
         await writer.WriteLineAsync("25 ICON \"" + textIcon.Text + "\"");
     if (checkAdmin.Checked)
     {
         String manifest = tempDir + @"\manifest.xml";
         createManifest(manifest);
         await writer.WriteLineAsync("55 24 \"" + manifest + "\"");
     }
}

String args2 = "/c \"" + Path.Combine(gccLocation, "windres.exe") + "\" -o \"" + Path.Combine(tempDir, "my.o").Replace("\\", "/") + "\" \"" + file.Replace("\\", "/") + "\"";

//Debug
//args2 = "/k echo " + args2;

ProcessStartInfo psi2 = new ProcessStartInfo();
psi2.FileName = "CMD.exe";
psi2.Arguments = args2;
psi2.UseShellExecute = false;
psi2.CreateNoWindow = true;

//Debug
//psi2.CreateNoWindow = false;

Process windres = Process.Start(psi2);
windres.WaitForExit();

if(windres.ExitCode != 0)
{
    MessageBox.Show("Error: Could not create resource file (" + windres.ExitCode + ")");
}
4

1 回答 1

0

最终使用批处理文件来运行命令。

                String args2 = "windres.exe -i \"" + Path.GetFullPath(file) + "\" -o \"" + Path.Combine(tempDir, "my.o") + "\"" ;

                using (StreamWriter writer = new StreamWriter(tempDir + @"\my.bat", false, Encoding.ASCII))
                {
                    await writer.WriteLineAsync("@echo off");
                    await writer.WriteLineAsync("cd " + Path.GetFullPath(gccLocation));
                    await writer.WriteLineAsync(args2);
                }

                //Debug
                //args2 = "/k echo " + args2;

                ProcessStartInfo psi2 = new ProcessStartInfo();
                psi2.FileName = tempDir + @"\my.bat";
                psi2.UseShellExecute = false;
                psi2.CreateNoWindow = true;

                //Debug
                //psi2.CreateNoWindow = false;

                Process windres = Process.Start(psi2);
                windres.WaitForExit();
于 2016-07-30T11:29:58.167 回答