编译.net .net Core程序 代码,仅做备份

//创建一个ProcessStartInfo对象 使用系统shell 指定命令和参数 设置标准输出  //编译.net core项目
            var psi = new ProcessStartInfo("dotnet", "build inputproject/Lord.Core.sln") { RedirectStandardOutput = true };
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            StringBuilder buildlog = new StringBuilder();
            //启动
            var proc = Process.Start(psi);
            if (proc == null)
            {
                Console.WriteLine("Can not exec.");
            }
            else
            {
                //开始读取
                using (var sr = proc.StandardOutput)
                {
                    while (!sr.EndOfStream)
                    {
                        Console.WriteLine(sr.ReadLine());
                        buildlog.Append(sr.ReadLine() + "\r\n");
                    }
                    if (!proc.HasExited)
                    {
                        proc.Kill();
                    }
                }
                if (proc.ExitCode == 0)
                {
                    Console.WriteLine("编译通过");
                }
                else
                {
                    Console.WriteLine("编译未通过");
                    Console.WriteLine(buildlog.ToString());
                }
            }
            var allfiles = Directory.GetFiles("inputproject", "*.dll", new EnumerationOptions() { RecurseSubdirectories = true });
            var filterfiles = new List<string>();
            var files = new List<string>();
            foreach (var item in allfiles)
            {
                if (!filterfiles.Contains(Path.GetFileName(item)))
                {
                    files.Add(item);
                    filterfiles.Add(Path.GetFileName(item));
                }
            }
            List<Type> lsttypes = new List<Type>();
            foreach (var item in files)
            {
                Assembly assembly = Assembly.LoadFile(Path.GetFullPath(item));
                try
                {
                    var types = assembly.GetTypes();
                    foreach (var tp in types)
                    {
                        if (tp.BaseType == typeof(System.Enum))
                        {
                            Console.WriteLine("Enum:" + tp.Name);
                        }
                        else if (tp.IsPublic && !tp.IsInterface && !tp.IsAbstract)
                        {
                            lsttypes.Add(tp);
                        }
                    }
                }
                catch
                {
                    continue;
                }
            }
            foreach (var tp in lsttypes)
            {
                Console.WriteLine(string.Concat("class:" + tp.FullName));
                foreach (var item in tp.GetProperties())
                {
                    Console.Write(item.Name + "|" + item.PropertyType + " ");
                }
                Console.WriteLine();
                Console.WriteLine();
            }
            Console.ReadKey();
编译.net core项目
            var process = new ProcessStartInfo("C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/MSBuild/15.0/Bin/MSBuild.exe", " E:/workZZF/src/SKIAD/LORD.SKIAD.WEB.sln /m /p:Platform=\"Any CPU\";CleanFile=\"true\";WarningLevel=0 /t:rebuild") { RedirectStandardOutput = true };
            var x = Process.Start(process);
            if (x == null)
            {
                Console.WriteLine("null");
                return;
            }
            //开始读取
            using (var sr = x.StandardOutput)
            {
                while (!sr.EndOfStream)
                {
                    Console.WriteLine(sr.ReadLine());
                    //buildlog.Append(sr.ReadLine() + "\r\n");
                }
                if (!x.HasExited)
                {
                    x.Kill();
                }
            }
            if (x.ExitCode == 0)
            {
                Console.WriteLine("编译通过");
            }
            else
            {
                Console.WriteLine("编译未通过");
                // Console.WriteLine(buildlog.ToString());
            }
            var allfiles = Directory.GetFiles("E:\\workZZF\\src\\SKIAD", "*.dll", new EnumerationOptions() { RecurseSubdirectories = true });
            var filterfiles = new List<string>();
            var files = new List<string>();
            foreach (var item in allfiles)
            {
                if (!filterfiles.Contains(Path.GetFileName(item)))
                {
                    files.Add(item);
                    filterfiles.Add(Path.GetFileName(item));
                }
            }
            List<Type> lsttypes = new List<Type>();
            foreach (var item in files)
            {
                try
                {
                    Assembly assembly = Assembly.LoadFile(Path.GetFullPath(item));
                    var types = assembly.GetTypes();
                    foreach (var tp in types)
                    {
                        if (tp.BaseType == typeof(System.Enum))
                        {
                            Console.WriteLine("Enum:" + tp.Name);
                        }
                        else if (tp.IsPublic && !tp.IsInterface && !tp.IsAbstract)
                        {
                            lsttypes.Add(tp);
                        }
                    }
                }
                catch
                {
                    continue;
                }
            }
            foreach (var tp in lsttypes)
            {
                Console.WriteLine(string.Concat("class:" + tp.FullName));
                foreach (var item in tp.GetProperties())
                {
                    try
                    {
                        Console.Write(item.Name + "|" + item.PropertyType + " ");
                    }
                    catch
                    {
                        continue;
                    }

                }
                Console.WriteLine();
            }
            / Console.ReadKey();
编译.net 项目 需要配置MSbuild.exe的位置,全路径

 

posted @ 2019-03-20 17:31  FreeTimeWorker  阅读(320)  评论(0编辑  收藏  举报