欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中实现在32位、64位系统下自动切换不同的SQLite dll文件

程序员文章站 2024-02-12 19:42:34
直接上代码: using system; using system.collections.generic; using system.windows.for...

直接上代码:

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;
      }
    }
  }
}