【问题标题】:Error 1001 system.badimageformatexception could not load file or assembly on creation of a setup project visual studio 2013错误 1001 system.badimageformatexception 在创建安装项目 Visual Studio 2013 时无法加载文件或程序集
【发布时间】:2014-10-21 23:09:58
【问题描述】:

我在 Visual Studio 2013 中有一个项目,它使用 wpf 和实体框架并连接到 SQL Server Express R2 数据库,该项目已完成,我需要为其创建设置,我正在使用 this tutorial
所以我创建脚本(带有数据)作为数据库的嵌入式资源,安装程序类(名为 SetupInstaller)并将项目的输出添加到安装程序,一切都没有错误地构建,但是当我尝试安装项目时我收到错误

错误 1001 System.BadImageFormatException 无法加载文件或程序集

我做了一些更改,这只是在我创建自定义操作时发生,但是当我不使用它时,不会创建数据库

我的连接字符串去

<connectionStrings>    
   <add name="Bulldog_Gym.Properties.Settings.BulldogGymConnectionString" 
     connectionString="Data Source=.;Initial Catalog=BulldogGym;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

我的 SetupInstaller 类是这样的

[RunInstaller(true)]
public partial class SetupInstaller : System.Configuration.Install.Installer
{
    SqlConnection masterConnection = new SqlConnection();
    public SetupInstaller()
        : base()
    {
        InitializeComponent();
    }
    private string GetSql(string Name)
    {

        try
        {
            // Gets the current assembly.
            Assembly Asm = Assembly.GetExecutingAssembly();

            // Resources are named using a fully qualified name.
            Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);

            // Reads the contents of the embedded file.
            StreamReader reader = new StreamReader(strm);
            return reader.ReadToEnd();

        }
        catch (Exception ex)
        {
            MessageBox.Show("In GetSQL: " + ex.Message);
            throw ex;
        }
    }

    private void ExecuteSql(string DatabaseName, string Sql)
    {
        System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, masterConnection);

        // Initialize the connection, open it, and set it to the "master" database
        masterConnection.ConnectionString = Properties.Settings.Default.BulldogGymConnectionString;
        Command.Connection.Open();
        Command.Connection.ChangeDatabase(DatabaseName);
        try
        {
            Command.ExecuteNonQuery();
        }
        finally
        {
            // Closing the connection should be done in a Finally block
            Command.Connection.Close();
        }
    }

    protected void AddDBTable(string strDBName)
    {
        try
        {
            // Creates the database.
            ExecuteSql("master", "CREATE DATABASE " + strDBName);

            // Creates the tables.
            ExecuteSql(strDBName, GetSql("bulldog.txt"));

            // Creates the stored procedure.
            //ExecuteSql(strDBName, GetSql("getproduct.txt"));

        }
        catch (Exception ex)
        {
            // Reports any errors and abort.
            MessageBox.Show("In exception handler: " + ex.Message);
            throw ex;
        }
    }


    public override void Install(System.Collections.IDictionary stateSaver)
    {
        base.Install(stateSaver);
        AddDBTable("BulldogGym");
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Uninstall(IDictionary savedState)
    {
        base.Uninstall(savedState);
        ExecuteSql("master", "DROP DATABASE BulldogGym");
    }
}

还有我的txt和数据库

我已经将数据库更改为非数据脚本,将 master 添加到连接字符串,将目标从 x64(当前)更改为 Any CPU,但这些似乎都不起作用,我需要一些指导。 谢谢!

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    听起来这与位数不匹配有关。我在这里回答了一个类似的问题:

    BadImageFormatException

    您的安装程序类程序集是 64 位,但尝试运行它的 installutil.exe 必须是 32 位,这会导致问题。不知何故,您需要执行 64 位版本的 installutil.exe。

    【讨论】:

    • 我将我的所有项目以及我正在使用的库更改为 x86,但仍然出现相同的错误,我仔细检查了项目并且所有内容都设置为 x86,我正在使用来自的外部库AForge 和 Secugen 指纹。还没有找到解决办法:(
    猜你喜欢
    • 2011-07-31
    • 2019-02-11
    • 1970-01-01
    • 2015-08-02
    • 2014-10-04
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多