繁体   English   中英

C#代码编译的.exe无法运行

[英]C# Code compiled .exe doesn't run

我正在使用CSharpCodeProvider来自己编译带有可变参数的.exe。 编译可以正常工作(不返回错误),并且可以成功,但是在运行时,它会启动并立即退出,而不会出现任何错误或输出。 更改“ Main”(例如更改为“私有”或通过重命名)时,编译器会输出没有有效的Main方法,因此示例代码不应成为原因。

有人对此有答案/解决方案吗? 我对此一无所知,希望得到您的任何帮助。 在此先谢谢〜

*编辑:

编译后的.exe输出: http : //imgur.com/a/WBvz3

编译:

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Resources;
using System.Security.Cryptography;
using System.Text;
using Microsoft.CSharp;
using Packer.Properties;

namespace Packer
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Sample Compiler");
            Console.WriteLine(".ico-path: ");
            var icon = "E:\\sample.ico"; //Console.ReadLine();
            Console.WriteLine("> " + icon);
            Console.WriteLine("Target-exe: ");
            var target = "E:\\sample.exe"; //Console.ReadLine();
            Console.WriteLine("> " + target);
            var source = Resources.samplesource;
            // Compile with all params
            var success = CompileFromSource(source, target, icon);
            // Determine result
            Console.WriteLine(success ? "Successfully compiled." : "Compiling error.");
            if (success) Process.Start(target);
            Console.ReadLine();
        }

        private static bool CompileFromSource(string source, string output,
string icon = null, string[] resources = null)
        {
            var cParams = new CompilerParameters
            {
                GenerateInMemory = true,
                WarningLevel = 0,
                GenerateExecutable = true,
                OutputAssembly = output
            };
            var options = "/optimize+ /platform:x86 /target:winexe /unsafe";
            if (icon != null)
                options += " /win32icon:\"" + icon + "\"";
            // Set the options.
            cParams.CompilerOptions = options;
            cParams.TreatWarningsAsErrors = false;
            cParams.ReferencedAssemblies.Add("System.dll");
            cParams.ReferencedAssemblies.Add("System.Core.dll");
            cParams.ReferencedAssemblies.Add("System.Data.dll");
            // Check if the user specified any resource files. & Add them
            if (resources != null && resources.Length > 0)
            {
                // Loop through all resource files specified in the Resources[] array.
                foreach (var res in resources)
                {
                    // Add each resource file to the compiled stub.
                    cParams.EmbeddedResources.Add(res);
                }
            }
            // Dictionary variable is used to tell the compiler what we want
            var providerOptions = new Dictionary<string, string> {{"CompilerVersion", "v4.0"}};
            var results = new CSharpCodeProvider(providerOptions).CompileAssemblyFromSource(cParams, source);
            // Check if any errors occured while compiling.
            if (results.Errors.Count <= 0) return true;
            Console.WriteLine("The compiler has encountered {0} errors", results.Errors.Count);
            foreach (CompilerError err in results.Errors)
            {
                Console.WriteLine("{0}\nLine: {1} - Column: {2}\nFile: {3}", err.ErrorText, err.Line, err.Column,
                    err.FileName);
            }
            return false;
        }
    }
}

要编译的代码:

using System;
using System.Text;

namespace CC2Runner
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                Debug.WriteLine("Sample Starting...");
                Console.WriteLine("Sample Starting...");
                ...
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.ReadLine();
        }
    }
}

您已经制作了WinForms程序。

它没有控制台,也没有任何内容输出到启动它的控制台。

我怎么知道? 这个:

var options = "/optimize+ /platform:x86 /target:winexe /unsafe";
                                        ^^^^^^^^^^^^^^

csc

 /target:exe                   Build a console executable (default) (Short
                               form: /t:exe)
 /target:winexe                Build a Windows executable (Short form:
                               /t:winexe)

改用/target:exe ,它应该会更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM