Hello,
How can i use the specific folder dll files in c#.
i write this code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ReportReader
{
public class AssemblyInfoItem
{
public string AssemblyName;
public string AssemblyFile;
public Assembly Assembly;
}
public class AssemblyResolveHelper
{
private List<AssemblyInfoItem> assemblyDomain;
public AssemblyResolveHelper()
{
assemblyDomain = new List<AssemblyInfoItem>();
assemblyDomain.Add(new AssemblyInfoItem
{
AssemblyName = "FirebirdSql.Data.FirebirdClient, Version=5.9.0.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c",
AssemblyFile = AppDomain.CurrentDomain.BaseDirectory + "Resources\\Binaries\\FirebirdSql.Data.FirebirdClient.dll"
});
assemblyDomain.Add(new AssemblyInfoItem
{
AssemblyName = "DevExpress.Data.v16.2, Version=16.2.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a",
AssemblyFile = AppDomain.CurrentDomain.BaseDirectory + "Resources\\Binaries\\DevExpress.Data.v16.2.dll"
});
assemblyDomain.Add(new AssemblyInfoItem
{
AssemblyName = "DevExpress.Utils.v16.2, Version=16.2.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a",
AssemblyFile = AppDomain.CurrentDomain.BaseDirectory + "Resources\\Binaries\\DevExpress.Utils.v16.2.dll"
});
assemblyDomain.Add(new AssemblyInfoItem
{
AssemblyName = "DevExpress.XtraEditors.v16.2, Version=16.2.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a",
AssemblyFile = AppDomain.CurrentDomain.BaseDirectory + "Resources\\Binaries\\DevExpress.XtraEditors.v16.2.dll"
});
assemblyDomain.Add(new AssemblyInfoItem
{
AssemblyName = "DevExpress.XtraGrid.v16.2, Version=16.2.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a",
AssemblyFile = AppDomain.CurrentDomain.BaseDirectory + "Resources\\Binaries\\DevExpress.XtraGrid.v16.2.dll"
});
}
public bool Load(bool isMessageShown)
{
var result = true;
foreach (var item in assemblyDomain)
{
try
{
var assemblyFile = Assembly.LoadFile(item.AssemblyFile);
item.Assembly = assemblyFile;
}
catch
{
result = false;
}
if (!result)
{
MessageBox.Show("Kütüphane (DLL) dosyası bulunamadı!" + Environment.NewLine + Environment.NewLine +
item.AssemblyName + Environment.NewLine + Environment.NewLine + item.AssemblyFile + Environment.NewLine,
"Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}
return result;
}
public Assembly Resolve(object sender, ResolveEventArgs args)
{
var result = (Assembly)null;
var assemblyItem = assemblyDomain.FirstOrDefault(item => item.AssemblyName == args.Name);
if (assemblyItem != null)
{
result = assemblyItem.Assembly;
}
return result;
}
}
}
and my program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
namespace ReportReader
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var assemblyResolve = new AssemblyResolveHelper();
assemblyResolve.Load(true);
AppDomain.CurrentDomain.AssemblyResolve += assemblyResolve.Resolve;
Application.Run(new Form1());
}
}
}
where do i make mistake?
can you help me?
What I have tried:
nothing special
reading specific dll folder