Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

How can i use the specific folder dll files in c#.

i write this code:
C#
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
C#
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
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var assemblyResolve = new AssemblyResolveHelper();
            assemblyResolve.Load(true);
            AppDomain.CurrentDomain.AssemblyResolve += assemblyResolve.Resolve;

            //DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection = true;
            Application.Run(new Form1());

        }
    }
}


where do i make mistake?

can you help me?

What I have tried:

nothing special
reading specific dll folder
Posted
Updated 15-May-17 9:22am
Comments
[no name] 15-May-17 12:50pm    
"can you help me":, yes, no, maybe. You would have have a problem first.
Member 10525430 15-May-17 12:53pm    
problem is when i run the exe files igot error
however i put the dll files debug directory the program is run
OriginalGriff 15-May-17 14:01pm    
What error? Remember, we can't see your screen...
Member 10525430 16-May-17 3:13am    
my problem is when the program is start,the exe control the directory which is include dll files however the exe is not use these dll files.it send error message " the exe stopped".When i copy the dll files on the exe's directory it works fine

1 solution

For constructing your string:
AssemblyFile = AppDomain.CurrentDomain.BaseDirectory + "Resources\\Binaries\\FirebirdSql.Data.FirebirdClient.dll"
I think you should use Path.Combine() otherwise a backslash might be missing.
Try stepping through your code and see what the value of the AssemblyFile string is.
 
Share this answer
 
Comments
Member 10525430 16-May-17 3:13am    
its not about assemblyfile string it comes true.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900