[已解决]指定的可执行文件不是此OS平台的有效应用程序 [英] [SOLVED]The specified executable is not a valid application for this OS platform

查看:3951
本文介绍了[已解决]指定的可执行文件不是此OS平台的有效应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个编辑器。在那个编辑器中,我调用WriteLog方法,它在我的程序中是硬编码的,带有一些字符串值。有编译程序文本的按钮。在那点击我试图保存名为Variable.cs的文件。比起我的代码我同时调用C#编译器来生成结果。





这是我保存文件的代码

  private   void  SaveCSFile()
{
MyCSFile = string .Empty;
// -------------------- ------- Class声明----------------------------- //
MyCSFile = @ using System;
class Variable

+ {;
MyCSFile + = ConvertGrid(dataGridView1);
MyCSFile + = ConvertGrid(dataGridView2);
MyCSFile + = @ public Variable() +
{;
MyCSFile + = T2.Text +
};
MyCSFile + = @ public void WriteLog(String Msg)
{
Console .WriteLine(Msg);
Console.ReadLine();
}
public string toString(string msg)
{
return msg.ToString();
}
}
;

// ---------------- ---------------------------------------------声明主要方法 - --------------- //
MyCSFile + = @ 类MainClass
{
;
MyCSFile + = @ static void Main() + \ n +
{;
MyCSFile + = @ 变量v = new Variable();;
// MyCSFile + =v。+ ConvertTab(tabControl3.SelectedTab.Text)+\ n;
MyCSFile + = v。 + T1.Text + \ n;
MyCSFile + = }};
MessageBox.Show(MyCSFile);
// -------------------- ---------编写CS文件--------------------------- //

writer = System.IO.File.CreateText(FilePath);
writer.WriteLine(MyCSFile.Trim()+ System.Environment.NewLine);
writer.Close();

}







此代码我在哪里调用C#编译器和传递源文件

  private   void  GenerateScript()
{
SaveCSFile();
// string AppName =demoFile.exe;
string AppName = tabControl3.SelectedTab.Text + 。exe;

// string mainClass =HelloWorld;
< span class =code-keyword> string
mainClass = tabControl3.SelectedTab.Text;

CSharpCodeProvider code = new CSharpCodeProvider();
ICodeCompiler compiler = code.CreateCompiler();
CompilerParameters param = new CompilerParameters();
// param.GenerateExecutable = true;
param.GenerateInMemory = ;
param.OutputAssembly = AppName.ToString();
param.MainClass = mainClass.ToString();
foreach (汇编asm AppDomain.CurrentDomain.GetAssemblies())
{
param.ReferencedAssemblies.Add(asm.Location);
}
string Mycode = MyCSFile;
// CompilerResults result = compiler.CompileAssemblyFromSource(param,Mycode);
CompilerResults result = compiler.CompileAssemblyFromFile(param, Variable.cs);
if (result.Errors.Count > 0
{
string error = 编译错误:\ n;
foreach (CompilerError错误 in result.Errors)
{
error + = err.ErrorText + + 行号: + err.Line + ;

}
MessageBox.Show( this ,错误, 错误);
}
其他
{
#region尝试to ExecuteApplication
尝试
{
if ( !System.IO.File.Exists(AppName))
{
MessageBox.Show( FileName + AppName + Not Found);
return ;
}

ProcessStartInfo pinfo = new ProcessStartInfo
{
FileName = AppName,
参数= FilePath,
UseShellExecute = false
};
Process.Start(AppName,FilePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
#endregion
}
}





这里我调用WriteLog方法并传递这样的字符串值

WriteLog(Hello World);

表格a记事本我创建..但我每次都收到错误

指定的可执行文件不是这个操作系统平台的有效应用程序

请帮助

谢谢.....

解决方案

请考虑使用CodeDOM。



请参阅我过去的回答:

使用CodeDom生成代码 [ ^ ],

创建使用可重新加载插件的WPF应用程序...... [ ^ ],

动态加载用户控件 [ ^ ],

现有实例上的C#Reflection InvokeMember [ ^ ]。



-SA

I have created an Editor. In That editor i am calling WriteLog Method, Which is hard coded in my program, with some string value. There is Button for With Compile Your Program Text. On that Click I am trying to save a file with name Variable.cs. Than From my code I am Calling C# Compiler at the same time to produce result.


This Is My Code to Save File

private void SaveCSFile()
        {
            MyCSFile = string.Empty;
            //---------------------------Class Declaration-----------------------------//
            MyCSFile = @"using System;
            class Variable"
            + "{";
            MyCSFile += ConvertGrid(dataGridView1);
            MyCSFile += ConvertGrid(dataGridView2);
            MyCSFile += @"public Variable()" +
                "{";
                   MyCSFile+= T2.Text +
                "}";
            MyCSFile += @"public void WriteLog(String Msg)
                    {
                         Console.WriteLine(Msg);
                        Console.ReadLine();
                    }
                     public string toString(string msg)
                     {
                        return msg.ToString();
                     }
                    }";

            //-------------------------------------------------------------Declaring Main Method-----------------//
            MyCSFile += @"class MainClass
                          {";
             MyCSFile+=@"static void Main()" + "\n" +
                        "{";
             MyCSFile += @"Variable v=new Variable();";
            //MyCSFile += "v."+ConvertTab(tabControl3.SelectedTab.Text) + "\n";
             MyCSFile += "v."+T1.Text+ "\n";
            MyCSFile += "}}";
           MessageBox.Show(MyCSFile);
           //-----------------------------Writing CS File---------------------------//

           writer = System.IO.File.CreateText(FilePath);
           writer.WriteLine(MyCSFile.Trim()+System.Environment.NewLine);
           writer.Close();

        }




This The Code Where I am Calling C# Compiler And Passing the Source File

private void GenerateScript()
        {
            SaveCSFile();
            //string AppName="demoFile.exe";
            string AppName = tabControl3.SelectedTab.Text + ".exe";
            
            //string mainClass="HelloWorld";
            string mainClass = tabControl3.SelectedTab.Text;

            CSharpCodeProvider code = new CSharpCodeProvider();
            ICodeCompiler compiler = code.CreateCompiler();
            CompilerParameters param = new CompilerParameters();
            //param.GenerateExecutable = true;
            param.GenerateInMemory = true;
            param.OutputAssembly = AppName.ToString();
            param.MainClass = mainClass.ToString();
            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                param.ReferencedAssemblies.Add(asm.Location);                
            }
            string Mycode = MyCSFile ;
            //CompilerResults result = compiler.CompileAssemblyFromSource(param, Mycode);
            CompilerResults result = compiler.CompileAssemblyFromFile(param, "Variable.cs");
            if (result.Errors.Count > 0)
            {
                string error = "Compilation error :\n";
                foreach (CompilerError err in result.Errors)
                {
                    error += err.ErrorText+" "+"In Line Number :"+err.Line +" ";
                   
                }
                MessageBox.Show(this, error, "Error");                
            }
            else
            {
                #region Try to ExecuteApplication
                try
                {
                    if (!System.IO.File.Exists(AppName))
                    {
                        MessageBox.Show("FileName" + AppName + " Not Found");
                        return;
                    }
                    
                    ProcessStartInfo pinfo = new ProcessStartInfo
                    {
                         FileName = AppName,
                        Arguments = FilePath,
                        UseShellExecute=false
                    };
                    Process.Start(AppName, FilePath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }
        }



here I call WriteLog Method And Passing a string value like this
WriteLog("Hello World");
Form a notepad i created.. But i am getting error every time
"The specified executable is not a valid application for this OS platform"
pls help
Thank you.....

解决方案

Please consider using CodeDOM instead.

Please see my past answers:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^],
Dynamically Load User Controls[^],
C# Reflection InvokeMember on existing instance[^].

—SA


这篇关于[已解决]指定的可执行文件不是此OS平台的有效应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆