您的位置:首页 > 数据库

C#中根据系统类型切换不同的SQLite DLL

2013-10-08 17:50 393 查看
搜索到两种方法

方法 一

原贴地址:/article/4954975.html

原来使用Win7的32位系统,进行C#工程的开发,后来重装系统,换成了win7的64位系统

调试原来的工程,由于在其中引用了“SQLite”的32位的dll,导致在64为位下程序无法运行(但是编译可以通过)

后来通过修改工程文件(.csproj),在其中设置引用的条件,解决了问题

打开引用了SQLite的工程(例如叫做info)的工程文件(info.csproj),找到对SQLite引用的语句,类似如下的代码

---------------------------------------------------------------------

<Reference Include="System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">

<SpecificVersion>False</SpecificVersion>

<HintPath>..\..\libs\SQLite\System.Data.SQLite.dll</HintPath>

</Reference>

---------------------------------------------------------------------

这里指定了引用的库的名称,路径等信息,将上面的配置内容,修改如下

---------------------------------------------------------------------

<Reference Condition=" '$(Platform)' == 'AnyCPU' " Include="System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">

<SpecificVersion>False</SpecificVersion>

<HintPath>..\..\libs\SQLite\System.Data.SQLite.dll</HintPath>

</Reference>

---------------------------------------------------------------------

只是增加了一个Condition的限制条件,表示只在平台类型位AnyCPU的时候,按照这个路径引用dll

复制上面的配置内容,并按照如下进行修改,设置在x64平台的生成时引用的dll路径

---------------------------------------------------------------------

<Reference Condition=" '$(Platform)' == 'x64' " Include="System.Data.SQLite.x64, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=AMD64">

<SpecificVersion>False</SpecificVersion>

<HintPath>..\..\libs\SQLite\System.Data.SQLite.x64.dll</HintPath>

</Reference>

---------------------------------------------------------------------

最后,说一下怎样在VS中设置不同的生成平台

在工具栏的“解决方案平台”(一般这里会显示着“Any CPU”)下拉框中选择“配置管理器”,在其中添加想要的平台类型(一般就在x64,x86,Itanium中选择,不要修改默认名称)

然后在窗口下部的“项目上下文中”,在对应的项目(这里仅info项目,其他的保持AnyCPU不变)中修改平台类型(修改为X64)

然后,配置到这里,可能在项目文件中,对应新增的平台类型的一些设置不完整(我遇到编译的时候提示“OutputPath没有设置”),这个时候,进入vs的项目属性界面,稍微修改一下(改成别的,保存,再改回来)这些设置

ok,这个时候,应该就可以根据平台的类型不同,引用到对应的dll了,

在网上找到的资料说,这种方法对于msi的安装程序的制作不太好使,我不太清楚,涉及到这方面的同学,再仔细查查看

不过,我这里提供一个可行的解决方案,那就是不要将整个项目制作到安装程序中,只是制作一个最基本的安装程序,然后通过升级来保证最新

如果是在没有网络的环境中,那也可以将编译产出单独解压到安装目录下,以避免将整个解决方案制作成安装包

方法 二

原贴地址:http://hi.baidu.com/12954537/item/10dc2dfc05b9d114d6ff8c0a

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Management;
using System.IO;
namespace SqliteAuto
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string dll32 = System.Windows.Forms.Application.StartupPath + "\\lib\\SQLite32.DLL";
string dll64 = System.Windows.Forms.Application.StartupPath + "\\lib\\SQLite64.DLL";
string dllpath = System.Windows.Forms.Application.StartupPath + "\\System.Data.SQLite.dll";
if (Detect32or64() == "32")
{
// do 32bit things.
try
{
using (FileStream fs = File.Create(dllpath)) { }
File.Copy(dll32, dllpath, true);
}

catch
{
Console.WriteLine("ERR");
}

}
else if (Detect32or64() == "64")
{
//do 64bit things
try
{
using (FileStream fs = File.Create(dllpath)) { }
File.Copy(dll64, dllpath, true);
}

catch
{
Console.WriteLine("ERR");
}
}
Application.Run(new Form1());
}

private static string Detect32or64()
{
try
{
string addressWidth = String.Empty;
ConnectionOptions mConnOption = new ConnectionOptions();
ManagementScope mMs = new ManagementScope("\\\\localhost", mConnOption);
ObjectQuery mQuery = new ObjectQuery("select AddressWidth from Win32_Processor");
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(mMs, mQuery);
ManagementObjectCollection mObjectCollection = mSearcher.Get();
foreach (ManagementObject mObject in mObjectCollection)
{
addressWidth = mObject["AddressWidth"].ToString();
}
return addressWidth;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return String.Empty;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: