C# 使用System.Data.SQLite(1.0.66.0)报混合模式程序集 “v2.0.50727” 错误的解决方法,有一定参考意义。

问题:

**C# 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集

解决方法:

这两天在使用System.Data.SQLite(1.0.66.0),报错误,上网查找具体解决方法就是修改bin文件夹下面的exe.config文件,在里增加 useLegacyV2RuntimeActivationPolicy="true"属性。具体如下。exe.config修改前如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<startup>
		<supportedRuntime version="v4.0" sku = ".NETFramework,Version=v4.0"/>
	</startup>
</configuration>

修改后如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<startup useLegacyV2RuntimeActivationPolicy="true">
		<supportedRuntime version="v4.0" sku = ".NETFramework,Version=v4.0"/>
	</startup>
</configuration>

以上是手动更改exe.config文件可以使程序正常运行,但是使用VS2022自动的打包程序,打包发布之后,这个exe.config文件会自动改回去。发布之后的文件并不能正常运行。又得手动改一下exe.config文件才能运行,这样给客户带来安装上的不便。
经过验证,可以在加载数据库之前检查exe.config的属性,是否存在“useLegacyV2RuntimeActivationPolicy="true"的内容,如果没有就增加这个属性,然后再加截数据库。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MAINFRAME
{
     class ConfigSettings
    {
        public static void ReadSetting()
        {
            XmlDocument doc = loadConfigDocument();

            XmlElement node = (XmlElement)doc.SelectSingleNode("//startup");
            XmlAttribute xmlAttribute = node.Attributes["useLegacyV2RuntimeActivationPolicy"];
            if(xmlAttribute ==null)
            {
                node.SetAttribute("useLegacyV2RuntimeActivationPolicy", "true");
                doc.Save(getConfigFilePath());
            }
        }


        private static XmlDocument loadConfigDocument()
        {
            XmlDocument doc = null;
            try
            {
                doc = new XmlDocument();
                doc.Load(getConfigFilePath());
                return doc;
            }
            catch (System.IO.FileNotFoundException e)
            {
                throw new Exception("No configuration file found.", e);
            }
        }

        private static string getConfigFilePath()
        {
            return Assembly.GetExecutingAssembly().Location + ".config";
        }

    }
}

在窗口加载的过程如下:

    SQLiteHelper mydata;//数据库连接、操作方法封装类
		private void Main_Load(object sender, EventArgs e)
		{
			this.WindowState = FormWindowState.Maximized;
			ConfigSettings.ReadSetting();//检查并调整exe.config文件,方法见上面代码。
            mydata = new SQLiteHelper(@Application.StartupPath + "\\datadb.db");
            Data_init();
         }

通过以上调整,就可以实现程序自动检查并调整exe.config文件属性,不需要再手动修改了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值