|
8 | 8 | using System.Diagnostics; |
9 | 9 | using System.Diagnostics.CodeAnalysis; |
10 | 10 | using System.Reflection; |
| 11 | +using System.Runtime.CompilerServices; |
11 | 12 | using Microsoft.Data.Sqlite.Properties; |
12 | 13 | using SQLitePCL; |
13 | 14 | using static SQLitePCL.raw; |
@@ -52,36 +53,40 @@ static SqliteConnection() |
52 | 53 | ?.GetRuntimeMethod("Init", Type.EmptyTypes) |
53 | 54 | ?.Invoke(null, null); |
54 | 55 |
|
| 56 | + var appDataType = Type.GetType("Windows.Storage.ApplicationData, Windows, ContentType=WindowsRuntime") |
| 57 | + ?? Type.GetType("Windows.Storage.ApplicationData, Microsoft.Windows.SDK.NET"); |
| 58 | + |
| 59 | + var storageFolderType = Type.GetType("Windows.Storage.StorageFolder, Windows, ContentType=WindowsRuntime") |
| 60 | + ?? Type.GetType("Windows.Storage.StorageFolder, Microsoft.Windows.SDK.NET"); |
| 61 | + |
| 62 | + object? currentAppData = null; |
55 | 63 | try |
56 | 64 | { |
57 | | - var currentAppData = Type.GetType("Windows.Storage.ApplicationData, Windows, ContentType=WindowsRuntime") |
58 | | - ?? Type.GetType("Windows.Storage.ApplicationData, Microsoft.Windows.SDK.NET") |
59 | | - ?.GetRuntimeProperty("Current")?.GetValue(null); |
| 65 | + currentAppData = appDataType?.GetRuntimeProperty("Current")?.GetValue(null); |
| 66 | + } |
| 67 | + catch (TargetInvocationException) |
| 68 | + { |
| 69 | + // Ignore "The process has no package identity." |
| 70 | + } |
60 | 71 |
|
61 | | - var localFolder = currentAppData?.GetType() |
62 | | - .GetRuntimeProperty("LocalFolder")?.GetValue(currentAppData); |
63 | | - var localFolderPath = (string?)localFolder?.GetType() |
64 | | - .GetRuntimeProperty("Path")?.GetValue(localFolder); |
| 72 | + if (currentAppData != null) |
| 73 | + { |
| 74 | + var localFolder = appDataType?.GetRuntimeProperty("LocalFolder")?.GetValue(currentAppData); |
| 75 | + var localFolderPath = (string?)storageFolderType?.GetRuntimeProperty("Path")?.GetValue(localFolder); |
65 | 76 | if (localFolderPath != null) |
66 | 77 | { |
67 | 78 | var rc = sqlite3_win32_set_directory(SQLITE_WIN32_DATA_DIRECTORY_TYPE, localFolderPath); |
68 | 79 | Debug.Assert(rc == SQLITE_OK); |
69 | 80 | } |
70 | 81 |
|
71 | | - var tempFolder = currentAppData?.GetType() |
72 | | - .GetRuntimeProperty("TemporaryFolder")?.GetValue(currentAppData); |
73 | | - var tempFolderPath = (string?)tempFolder?.GetType() |
74 | | - .GetRuntimeProperty("Path")?.GetValue(tempFolder); |
| 82 | + var tempFolder = appDataType?.GetRuntimeProperty("TemporaryFolder")?.GetValue(currentAppData); |
| 83 | + var tempFolderPath = (string?)storageFolderType?.GetRuntimeProperty("Path")?.GetValue(tempFolder); |
75 | 84 | if (tempFolderPath != null) |
76 | 85 | { |
77 | 86 | var rc = sqlite3_win32_set_directory(SQLITE_WIN32_TEMP_DIRECTORY_TYPE, tempFolderPath); |
78 | 87 | Debug.Assert(rc == SQLITE_OK); |
79 | 88 | } |
80 | 89 | } |
81 | | - catch |
82 | | - { |
83 | | - // Ignore "The process has no package identity." |
84 | | - } |
85 | 90 | } |
86 | 91 |
|
87 | 92 | /// <summary> |
|
0 commit comments