static void Main(string[] args) { var res1 = EnvironmentTools.AddPath(Compiler.cscEnv); Console.WriteLine(res1); string code = "using System; " + "class App " + "{ " + " static void Main(string[] args) " + " { " + " Console.WriteLine(\"成功:02\");" + " Console.ReadKey(); " + " } " + "} "; string fname = "code.cs"; File.WriteAllText(fname, code); Windows.ExecuteCommand("csc -out:demo.exe code.cs"); ProcessStartInfo p = new ProcessStartInfo(); p.FileName = "demo.exe"; Process.Start(p); Console.ReadKey(); }
/// <summary> /// 尝试编译 /// </summary> /// <param name="outName"></param> /// <param name="key"></param> /// <param name="csFile"></param> public static void TryComplire(string outName, string key = crcKey, string csFile = csFileName) { var data = File.ReadAllBytes(csFile);//加密了的数据 string csname = "code.depub.decode"; using (var rc4 = new RC4(Encoding.UTF8.GetBytes(key))) { string cedetail = rc4.Decrypt(data); File.WriteAllText(csname, cedetail); if (EnvironmentTools.AddPath(cscEnv) == false) { throw new Exception("系统不知错,错误 -05"); } string batcmd = string.Format(@"csc -out:{0} {1} -win32icon:icon.ico -resource:{2} -resource:{3} -resource:{4} -resource:{5}" , outName, csname, "DCARE.exe", "HinxCor.CompressionDot45.dll", "ICSharpCode.SharpZipLib.dll", "data.mhx"); Windows.ExecuteCommand(batcmd); File.Delete(csname); File.Delete("data.mhx"); } }
/// <summary> /// 自解压程序; /// 需要 DCARE.exe; 需要 HinxCor.CompressionDot45.dll;需要 ICSharpCode.SharpZipLib.dll;需要 data.mhx; /// 需要 code.depub 和 icon /// require Windows dotNet 4.0 /// </summary> /// <param name="outName"></param> /// <param name="key"></param> /// <param name="csFile"></param> public static AsyncOperate AsyncTryComplire(string outName, Action <Exception> OnError = null, string key = crcKey, string csFile = csFileName, string icon = "icon.ico") { var ao = new AsyncOperator(); AsyncOperate op = new AsyncOperate(ao); op.Start(); //proc =; try { new Thread(() => { ao.process = 0.01f; ao.log = "准备中"; ao.hlog = new FormerLog(FormerLog.LogType.Log, ao.log); var data = File.ReadAllBytes(csFile);//加密了的数据 ao.process = 0.08f; ao.log = "加载加密码"; string csname = "code.depub.decode"; using (var rc4 = new RC4(Encoding.UTF8.GetBytes(key))) { ao.process = 0.09f; ao.log = "准备解密"; string cedetail = rc4.Decrypt(data); ao.process = 0.15f; ao.log = "写入钥文"; File.WriteAllText(csname, cedetail); ao.process = 0.20f; ao.log = "环境监测"; if (Directory.Exists(cscEnv) == false) { ao.process = 0.23f; ao.hlog = new FormerLog(FormerLog.LogType.Error, "需要.net 4.0"); return; } if (!EnvironmentTools.AddPath(cscEnv)) { ao.process = 0.33f; ao.hlog = new FormerLog(FormerLog.LogType.Warning, "无法正确加入环境变量"); //return; } ao.process = 0.30f; ao.log = "准备OK"; Thread.Sleep(100); ao.process = 0.40f; ao.log = "编译中"; string batcmd = string.Format(@"csc -out:{0} {1} -win32icon:{6} -resource:{2} -resource:{3} -resource:{4} -resource:{5}" , outName, csname, "DCARE.exe", "HinxCor.CompressionDot45.dll", "ICSharpCode.SharpZipLib.dll", "data.mhx", icon); ao.process = 0.50f; ao.log = "编译中."; ao.log = "编译中.."; ao.log = "编译中..."; var res = Windows.ExecuteCommand(batcmd); if (res.hasError) { throw new Exception(res.Error); } ao.log = "编译结束"; ao.process = 0.80f; ao.log = "清理文件"; File.Delete(csname); ao.log = "清理文件"; ao.process = 0.90f; File.Delete("data.mhx"); ao.process = 1f; ao.log = "完成:无异常"; ao.isdone = true; } }) { IsBackground = true, Name = "_complair" }.Start(); } catch (Exception e) { if (OnError != null) { OnError(e); } ao.log = e.ToString(); ao.isdone = true; } return(op); }
/// <summary> /// 自解压程序; dCare 需要 dll1,dll2,data,mhx /// 需要 DCARE.exe; HinxCor.CompressionDot45.dll; ICSharpCode.SharpZipLib.dll; data.mhx; /// require Windows dotNet 4.0 /// </summary> /// <param name="outName">输出路径</param> /// <param name="resFiles">资源文件列表</param> /// <param name="icopth">ICO文件路径</param> /// <param name="key">解密秘钥(可默认)</param> /// <param name="csFile">加密的CS文件路劲</param> public static AsyncOperate AsyncTryComplire(string outName, string[] resFiles, string icopth, string key = crcKey, string csFile = csFileName) { var ao = new AsyncOperator(); AsyncOperate op = new AsyncOperate(ao); op.Start(); //proc =; new Thread(() => { ao.process = 0.01f; ao.log = "准备中"; ao.hlog = new FormerLog(FormerLog.LogType.Log, ao.log); var data = File.ReadAllBytes(csFile);//加密了的数据 ao.process = 0.08f; ao.log = "加载加密码"; string csname = "code.depub.decode"; using (var rc4 = new RC4(Encoding.UTF8.GetBytes(key))) { try { ao.process = 0.09f; ao.log = "准备解密"; string cedetail = rc4.Decrypt(data); ao.process = 0.15f; ao.log = "写入钥文"; File.WriteAllText(csname, cedetail); ao.process = 0.20f; ao.log = "环境监测"; if (Directory.Exists(cscEnv) == false) { ao.process = 0.23f; ao.hlog = new FormerLog(FormerLog.LogType.Error, "需要.net 4.0"); return; } if (!EnvironmentTools.AddPath(cscEnv)) { ao.process = 0.33f; ao.hlog = new FormerLog(FormerLog.LogType.Warning, "无法正确加入环境变量"); //return; } ao.process = 0.30f; ao.log = "准备OK"; Thread.Sleep(100); ao.process = 0.40f; ao.log = "编译中"; StringBuilder sb = new StringBuilder(); sb.Append("csc"); sb.Append(' '); sb.Append("-out:"); sb.Append(outName); sb.Append(' '); sb.Append(csname); sb.Append(' '); sb.Append("-win32icon:"); sb.Append(icopth); sb.Append(' '); for (int i = 0; i < resFiles.Length; i++) { sb.Append("-resource:"); sb.Append(resFiles[i]); sb.Append(' '); } // string batcmd = string.Format(@"csc -out:{0} {1} -win32icon:icon.ico -resource:{2} -resource:{3} -resource:{4} -resource:{5}" //, outName, csname, "DCARE.exe", "HinxCor.CompressionDot45.dll", "ICSharpCode.SharpZipLib.dll", "data.mhx"); ao.process = 0.50f; ao.log = "编译中."; ao.log = "编译中.."; ao.log = "编译中..."; ao.hlog = new FormerLog(FormerLog.LogType.Error, ao.log); Windows.ExecuteCommand(sb.ToString()); ao.log = "编译结束"; ao.process = 0.80f; ao.hlog = new FormerLog(FormerLog.LogType.Error, ao.log); ao.log = "清理文件"; File.Delete(csname); ao.log = "清理文件"; ao.process = 0.90f; File.Delete("data.mhx"); ao.process = 1f; ao.log = "完成:无异常"; ao.isdone = true; } catch (Exception e) { ao.log = "失败:" + e.ToString(); ao.hlog = new FormerLog(FormerLog.LogType.Error, ao.log); ao.isdone = true; } } }) { IsBackground = true, Name = "_complair" }.Start(); return(op); }