在调试文件夹外部运行时找不到DLL

时间:2016-03-10 11:58:26

标签: c# dll

应用程序名称,路径和网址已重命名为“MyApp”和“示例”,只是为了便于阅读。

您好,我目前在我的应用程序中使用1个dll文件,这是c#中的log4net。现在我将我的dll包含在

的引用中
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    </style>
    <style name="No_Title_AppTheme" parent="android:Theme.Light.NoTitleBar">

    </style>
</resources>

仅仅因为我将公开发布我的申请。现在它在调试模式和内部工作正常,但是当我在/ bin / debug文件夹外运行exe时会抛出错误..

  

未处理的异常:System.IO.FileNotFoundException:无法加载   文件或程序集'log4net,Version = 1.2.15.0,Culture = neutral,   PublicKeyToken = 669e0ddf0bb1aa2a'或其依赖项之一。该   系统找不到指定的文件。          在MyApp.Program.Main(String [] args)

我还把这个代码放在其中我认为会阻止它发生..但我做错了什么?这段代码应该涵盖我的**

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

但是当在/ bin / debug之外运行exe solo时,它甚至不显示“我们下载一些文件时请等待......”行

2 个答案:

答案 0 :(得分:0)

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

它对我有用。

答案 1 :(得分:0)

当我遇到这个问题时,我将.dll部署到可执行文件的运行位置。将dll添加到项目Resources并将其Build Action设置为Content。

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被称为&#34; Microsoft.Win32.TaskScheduler.dll&#34;如果是这种情况,使用您的应用程序,当您的DLL作为资源添加时,句点将替换为下划线。确保在调用FindResourceByName时反映这一点,否则您的查找将失败:

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