c#控制台应用程序部署

时间:2016-04-01 21:57:23

标签: c# dll include scheduled-tasks

我有一个应用程序,由于其性质,需要在工作时间之后运行,以免中断用户的工作流程。我已经下载并添加了对Microsoft.Win32.TaskScheduler.dll的引用,根据有关安排任务在当天晚些时候运行的最佳方式的另一个问题的建议。

在调试中,程序按预期工作,但在部署时,我收到以下错误:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Win32.TaskScheduler, Version=2.5.16.0, Culture=neutral, PublicKeyToken=0d013ddd5178a2ae' or one of its dependencies.

这让我相信dll在构建时没有正确添加到可执行文件中。

我已采取措施解决此问题:

  1. 在解决方案资源管理器中,确保Microsoft.Win32.TaskScheduler Copy Local属性为True
  2. 项目属性,发布,应用程序文件 - Microsoft.Win32.TaskScheduler.dll包含在发布中,需要下载,包含哈希
  3. 删除依赖项并重新添加
  4. 遵循this answer
  5. 中的建议

    此时所有人都失败了。我可以确认.dll是否应该在/ bin / debug文件夹中。另外,我以相同的方式手动添加System.Management.Automation,它似乎按预期运行。

    如果有人有任何其他建议,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

我曾尝试使用derpirscher的解决方案,但我最终使用的解决方案是将dll部署到程序的运行目录中。它不是最优雅的解决方案,但它满足“单文件部署”的要求。然而,应该注意,在部署到的所有系统上,用户帐户是管理员。如果运行程序的用户不是管理员,则此解决方案可能无效,具体取决于运行可执行文件的位置(例如,另一个用户的帐户文件夹)。

class Program
{
    //Deploys the DLL to the location of the executable, as suggested by B. Clay Shannon
    //The Program() method runs before Main() and allows for regisration or placement
    //of files before the program "starts". Placing this line in Main()
    //causes a FileNotFound exception when it tries to register the assembly.
    static Program()
    {
        //The dll has been added as a resource, build action "Content".
        //Note the underscores in "Microsoft_Win32_TaskScheduler"
        //Adding the dll to the resource manager replaces '.' with '_' in the resource name
        File.WriteAllBytes(string.Format("{0}{1}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "\\Microsoft.Win32.TaskScheduler.dll"), FindResourceByName("Microsoft_Win32_TaskScheduler"));
    }

    static void Main(string[] args)
    {
        ...
    }

    /// <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));
    }
}
相关问题