最近,在写一个C#版本的protoc转换工具,发现死活转不出来,报各种奇奇怪怪的错误,如missing input file等。不得不说,win版本的protoc设计很烂~~,先贴教程里唯一提到的一行:
protoc -I=$SRC_DIR --csharp_out=$DST_DIR $SRC_DIR/addressbook.proto
命令行下,$SRC_DIR是 ".proto"文件所在目录,$DST_DIR是输出目录,最后是$SRC_DIR/xx.proto,要严格遵循这种参数格式,才能指定绝对路径。
var file = Path.GetFullPath(textBox1.Text);
var fileName = Path.GetFileName(file);
var workDir = file.Replace(fileName,"");
string src = textBox1.Text;
string outPath = textBox3.Text;
string args = string.Format("-I {0} --csharp_out {1} {2}",workDir,outPath,file);
// 运行新进程
Process process = new Process();
process.StartInfo.FileName = _complilerPath;
process.StartInfo.UseShellExecute = false; // 是否使用外壳程序
process.StartInfo.CreateNoWindow = true; //是否在新窗口中启动该进程的值
process.StartInfo.RedirectStandardInput = true; // 重定向输入流
process.StartInfo.WorkingDirectory = Application.StartupPath;
process.StartInfo.RedirectStandardOutput = true; //重定向输出流
process.StartInfo.RedirectStandardError = true; //重定向错误流
process.StartInfo.Arguments = args;
process.Start();
string error = process.StandardOutput.ReadToEnd();//获取输出信息
error = process.StandardError.ReadToEnd();//获取输出信息
if(!string.IsNullOrEmpty(error))
{
listView1.Items.Add("[Error]"+error);
}
process.WaitForExit();
process.Close();
listView1.Items.Add(file);
listView1.Items.Add("转换完毕!");