背景:金蝶dll是.net framework, 在.net core 項目不兼容,這里說明控制台的方式
(另一種做法,再弄一個.net framework網站項目負責調用金蝶服務)
主程序 .Net Core
private static string CMDRun(string command) { var dir = AppDomain.CurrentDomain.BaseDirectory + @"\api\"; if (!System.IO.Directory.Exists(dir)) { return "api目錄不存在"; } var path = System.IO.Directory.GetFiles(dir, "*.exe").FirstOrDefault(); if (string.IsNullOrEmpty(path)) { return "api目錄不存在exe程序"; } var root = System.IO.Directory.GetDirectoryRoot(path); var fileName = System.IO.Path.GetFileName(path); using (Process p = new Process()) { p.StartInfo.CreateNoWindow = true; p.StartInfo.FileName = @"cmd.exe"; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false;//將此屬性設置為 false 使您能夠重定向輸入流、輸出流和錯誤流 p.Start(); p.StandardInput.WriteLine(@"echo off ");//不顯示命令 p.StandardInput.WriteLine(root.Replace("\\", "")); //先轉項目所在的盤 p.StandardInput.WriteLine($@"cd {dir}"); //再轉到exe所在目錄下 p.StandardInput.WriteLine($"{fileName} {command}");//執行exe p.StandardInput.WriteLine("exit"); string result = p.StandardOutput.ReadToEnd(); var array = result.Split("\r\n"); p.Close(); p.Dispose(); return array[array.Length - 3]; } }
控制台 .Net Framework
private static void Main(string[] args) { if (args[0] == "GetProductList") { if (args.Length < 3) { Console.WriteLine("需要參數[methodName,StartRow,Limit]"); return; } int.TryParse(args[1], out int StartRow); int.TryParse(args[2], out int Limit);
//具體的事,比如這里是金蝶服務 string resultStr = api_k3_SS.GetProductList(StartRow, Limit);
Console.WriteLine(resultStr);
}
}
