2016-03-10 48 views
0

为了便于阅读,应用程序名称,路径和URL已重命名为'MyApp'和'example'。在调试文件夹外部运行时未找到DLL

你好,我目前使用1 DLL文件为我的应用程序,这是在C#中的log4net。现在我包括我的dll在参考

C:\Users\ashle\AppData\Roaming\MyApp 

只是因为我会公开发布我的申请。现在,它工作正常之外调试模式里面,但是当我运行它抛出一个错误/斌/ debug文件夹外的exe ..

未处理的异常:System.IO.FileNotFoundException:未能加载 文件或程序集'log4net,版本= 1.2.15.0,文化=中立, PublicKeyToken = 669e0ddf0bb1aa2a'或其依赖项之一。 系统找不到指定的文件。 在MyApp.Program.Main(字串[] args)

我也把这个代码中,我认为会阻止它发生..但我究竟做错了什么?此代码应支付我一个**

if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/MyApp")) 
       { 
        Console.WriteLine("Created directory: " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/MyApp"); 
        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/MyApp"); 
       } 

       if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/MyApp/log4net.dll")) 
       { 
        Console.WriteLine("Please wait while we download some files..."); 

        string downloadUrl = "http://example.com"; 

        if (checkWebsiteAvalibility(downloadUrl)) 
        { 
         WebClient webClient = new WebClient(); 

         webClient.DownloadFileAsync(new Uri(downloadUrl + "/downloads/log4net.dll"), 
          Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/MyApp/log4net.dll"); 

         Console.Clear(); 

但外运行的exe独奏时/斌/调试它甚至不显示“请稍候,我们下载一些文件...”行

+2

发布一个.exe的时候,你必须公布所有相关的.dll文件,以及在同一目录 –

+0

我想你,你之前使用日志记录甚至可以打印出“请稍等,我们下载一些文件......” – Domysee

回答

0

在发布您的项目之前,请从您的参考中删除该dll文件。通过添加引用添加相同的dll。然后尝试发布它。

它为我工作。

0

当我遇到此问题时,我将.dll部署到可执行文件的运行位置。将dll添加到项目资源中,并将其“生成操作”设置为“内容”。

class Program 
{ 
    //Programmatically, the Program() method is called prior to Main() and before any registrations are made. 
    //This is where we write the dll to the disk. 
    static Program() 
    { 
     //Path.GetDirectoryName() returns the folder path to a particular file. 
     //Assembly.GetExecutingAssembly().Location returns the path to the current running location of the compiled executable, with the name. E.G. C:\MyProject\MyProgram.exe 
     //We combine this with Path.GetDirectoryName to get the folder, and then write the dll into this folder. That way, when this method finishes and main is called, it will find the dll in the folder. 
     File.WriteAllBytes(string.Format("{0}{1}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "\\log4net.dll"), FindResourceByName("log4net")); 
    } 

    /// <summary> 
    /// Returns a byte array of the object searched for 
    /// </summary> 
    /// <param name="objectName">Name of the resource object</param> 
    /// <returns>Byte array of the specified resource object</returns> 
    private static byte[] FindResourceByName(string objectName) 
    { 
     object obj = Properties.Resources.ResourceManager.GetObject(objectName); 
     return ((byte[])(obj)); 
    } 

    //Rest of your code goes here... 
} 

在我的情况,我的DLL被称为“Microsoft.Win32.TaskScheduler.dll”如果是这样的话,你的应用程序,期间会被替换为下划线时,您的DLL作为资源添加。请确保您调用FindResourceByName或您的查找失败时,这反映:

File.WriteAllBytes(string.Format("{0}{1}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "\\Microsoft.Win32.TaskScheduler.dll"), FindResourceByName("Microsoft_Win32_TaskScheduler")); 
相关问题