-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathNativeDependencies.cs
More file actions
92 lines (79 loc) · 2.77 KB
/
NativeDependencies.cs
File metadata and controls
92 lines (79 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Configuration;
using System.IO;
using System.Security;
namespace HDF.PInvoke
{
public static class NativeDependencies
{
public const string NativePathSetting = "NativeDependenciesAbsolutePath";
public static void ResolvePathToExternalDependencies()
{
string NativeDllPath;
if (!GetDllPathFromAppConfig(out NativeDllPath))
{
GetDllPathFromAssembly(out NativeDllPath);
}
AddPathStringToEnvironment(NativeDllPath);
}
//----------------------------------------------------------------
private static bool GetDllPathFromAppConfig(out string aPath)
{
aPath = string.Empty;
try
{
string pathFromAppSettings = ConfigurationManager.AppSettings[NativePathSetting];
if (string.IsNullOrEmpty(pathFromAppSettings))
return false;
foreach (var c in Path.GetInvalidPathChars())
{
if (pathFromAppSettings.Contains( new string(c, 1) ))
return false;
}
aPath = pathFromAppSettings;
return true;
}
catch
{
return false;
}
}
private static string GetAssemblyName()
{
string myPath = new Uri(System.Reflection.Assembly
.GetExecutingAssembly().CodeBase).AbsolutePath;
myPath = Uri.UnescapeDataString(myPath);
return myPath;
}
private static void GetDllPathFromAssembly(out string aPath)
{
switch (IntPtr.Size)
{
case 8:
aPath = Path.Combine(Path.GetDirectoryName(GetAssemblyName()), Constants.DLL64bitPath);
break;
case 4:
aPath = Path.Combine(Path.GetDirectoryName(GetAssemblyName()), Constants.DLL32bitPath);
break;
default:
throw new NotImplementedException();
}
}
private static void AddPathStringToEnvironment(string aPath)
{
try
{
string EnvPath = Environment.GetEnvironmentVariable("PATH");
if (EnvPath.Contains(aPath)) return;
Environment.SetEnvironmentVariable("PATH", aPath + ";" + EnvPath);
System.Diagnostics.Trace.WriteLine(string.Format(
"{0} added to Path.", aPath));
}
catch(SecurityException)
{
System.Diagnostics.Trace.WriteLine(
"Changing PATH not allowed");
}
}
}
}