C# 调用6S.exe
代码来自此文章https://blog.csdn.net/summer_dew/article/details/91988258
我做了一点点修改,增加了输出结果到文档的功能,正则表达式实在是写不来,笨方法用字符串的方法截取的,输出大气校正的结果
输入文件为in.txt, 输出文件为out.txt
运行 调用6S模型.exe 即可
代码:
- static void Main(string[] args)
- {
- string SixSexe_dir = System.AppDomain.CurrentDomain.BaseDirectory; //项目文件夹
- Console.WriteLine(SixSexe_dir);
- string SixSexe_path = SixSexe_dir + "\6S.exe"; //6s.exe放在了bin/debug/6SModel目录下
- string intxt_path = SixSexe_dir + "\in.txt"; //参数文件
- try
- {
- using (Process myProcess = new Process())
- {
- myProcess.StartInfo.FileName = SixSexe_path; //exe的路径
- myProcess.StartInfo.RedirectStandardInput = true; //接受来自调用程度的输入
- myProcess.StartInfo.RedirectStandardOutput = true; //由调用程度获取输出信息
- myProcess.StartInfo.UseShellExecute = false; //是否使用操作系统的Shell启动
- myProcess.StartInfo.CreateNoWindow = true; //不显示调用程序的窗口
- myProcess.Start(); //开启进程
- // 向进程中输出参数
- StreamWriter myStreamWriter = myProcess.StandardInput;
- String inputText;
- int numLines = 0;
- FileStream fs = new FileStream(intxt_path, FileMode.Open);//参数文件,按行排列。
- StreamReader sr = new StreamReader(fs);
- inputText = sr.ReadLine();
- while (inputText != null)
- {
- myStreamWriter.WriteLine(inputText);//程序的核心,向目标程序中写入数据。
- inputText = sr.ReadLine();
- numLines++;//行数统计
- }
- fs.Close();
- myStreamWriter.Close();
- // 获得输出内容
- string out_str = myProcess.StandardOutput.ReadToEnd();
- // Console.WriteLine(out_str);
- FileStream outToFile = new FileStream(SixSexe_dir + "\out.txt", FileMode.OpenOrCreate);
- StreamWriter streamWriter = new StreamWriter(outToFile);
- streamWriter.Write(out_str);
- streamWriter.Close();
- // 提取目标结果
- string[] lines = out_str.Split('
- ');
- string xa_xb_xc = lines[163];
- for (int i = 155; i <= 167; i++)
- {
- Console.WriteLine(lines[i]);
- }
- string extra1 = xa_xb_xc.Substring(49, 7);
- string extra2 = xa_xb_xc.Substring(58, 7);
- string extra3 = xa_xb_xc.Substring(67, 7);
- Console.WriteLine(extra1);
- Console.WriteLine(extra2);
- Console.WriteLine(extra3);
- myProcess.Close();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- Console.ReadLine();
- }
附件:Debug.zip
顺便把6S手册也挂出来吧,官网不好下,文件分成了三部分包括原理,代码介绍等,全英文看懂不容易。