gpt4 book ai didi

c# - 未捕获 System.Data.SQLite 的 FileNotFoundExceptions

转载 作者:行者123 更新时间:2023-11-30 12:11:35 25 4
gpt4 key购买 nike

我在我的项目中使用 System.Data.SQLite。当输出文件夹中没有 System.Data.SQLite dll 时,我无法捕获 FileNotFoundException(其他异常捕获正常)。这是代码示例:

    private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
SQLiteConnection conn = new SQLiteConnection();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

MessageBox 没有显示。如果我在单独的函数中提取此代码并将此函数调用包装在 try catch 中,则捕获异常工作正常并且 MessageBox 显示:

    private void DeclareConnection()
{
SQLiteConnection conn = new SQLiteConnection();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
DeclareConnection();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

问题是什么?

最佳答案

您将不得不处理 AppDomain.AssemblyResolve事件,

订阅AssemblyResolve事件

AppDomain.CurrentDomain.AssemblyResolve += HandleAssemblyResolve;

这里是一些示例代码,用于处理 c# 中 x86/x64 SQLite 程序集的加载

    public static Assembly HandleAssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.Contains("System.Data.SQLite"))
{
if (_assembliesResolved)
return null;

Assembly returnValue;

string executingAssemblyPath = Assembly.GetExecutingAssembly().Location;
executingAssemblyPath = Path.GetDirectoryName(executingAssemblyPath);

if (Environment.Is64BitProcess)
executingAssemblyPath = Path.Combine(executingAssemblyPath, @"lib-sqlite\x64\", "System.Data.SQLite.dll");
else //32 bit process
executingAssemblyPath = Path.Combine(executingAssemblyPath, @"lib-sqlite\x86\", "System.Data.SQLite.dll");

returnValue = Assembly.LoadFrom(executingAssemblyPath);

_assembliesResolved = true;

return returnValue;
}

return null;
}

关于c# - 未捕获 System.Data.SQLite 的 FileNotFoundExceptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15197587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com