wpf 如何在VisualStudio中生成项目时将项目DLL自动发布到文件夹

x9ybnkn6  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(249)

我的应用程序中有多个模块,我想将模块的程序集动态复制到主启动项目的bin文件夹中,而不添加为引用。
我能够使用Visual Studio中的“发布”选项手动发布项目的DLL。但是我希望在构建模块项目时能够自动将模块的程序集复制到主启动项目bin文件夹中。
除了使用PostBuild事件之外,还有其他选择吗?
如果你能分享一下你的想法,那将是非常有帮助的。

4ioopgfo

4ioopgfo1#

不确定是否有更优雅的方式来处理dotnet,但你总是可以在Windows上创建Batch *.bat文件(或Linux中的Bash脚本)并编写任何你想要的命令,如下所示:

dotnet build <your_main_project_name> 
dotnet publish <your_second_project_name> 
copy <path_to_second_bin> <path_to_target_bin>

当然,也有特殊的PowerShell脚本文件等,但总体思路是使用脚本。您可以查找指南或检查一些Batch commandsBash commands。这可能是老式的,但总是工作的方式

ruyhziif

ruyhziif2#

您可以编写一个控制台程序,以便能够将程序集复制到主启动项目bin文件夹中,但不幸的是,这仍然使用PostBuild。
控制台程序如下:

public class Program
 {
     public static void Main(string[] args)
     {
         //string para1 = @"D:\word_code\Server\myProj\\bin,casereport,D:\Web_zy\Bin";// followed by source, dll file keywords, target directory
         string para1 =args[0];
         string[] ar = para1.Split(',', '|', ',');//source project bin path, keyword of mobile dll file, target server path
         Console.WriteLine("Source: " + ar[0]);
         Console.WriteLine("Target: " + ar[2]);
         var moveFile = new List<string>();
         if (!string.IsNullOrWhiteSpace(ar[0]) && !string.IsNullOrWhiteSpace(ar[1]))
         {
             var list=GetFiles(ar[0],"", true);
             moveFile = (from p in list
                     where Path.GetFileName(p).ToLower().Contains(ar[1].ToLower())
                     select p).ToList();
         }
         if (!string.IsNullOrWhiteSpace(ar[2]))
         {
             foreach (var item in moveFile)
             {
                 CopyFile(item, $@"{ar[2]}\{Path.GetFileName(item)}");
             }
         }
         Console.ForegroundColor = ConsoleColor.Green;
         Console.Write("[Success] Press any key to exit...");
         Console. ReadKey(true);
     }

     #region Find all files in a directory that contains subdirectories
     /// <summary>
     /// Get all files in the directory or the specified file type file address
     /// </summary>
     public static List<string> fileList = new List<string>();
     public static string[] GetFiles(string fullPath, string extName, bool isFullName = false)
     {
         try
         {
             fileList. Clear();
             DirectoryInfo dirs = new DirectoryInfo(fullPath); //Get the directory object of the path where the program is located
             DirectoryInfo[] dir = dirs.GetDirectories();//Get the folder object under the directory
             FileInfo[] file = dirs.GetFiles();//Get the file object under the directory
             int dircount = dir.Count();//Get the number of folder objects
             int filecount = file.Count();//Get the number of file objects

             // loop through folders
             for (int i = 0; i < dircount; i++)
             {
                 string pathNode = fullPath + "\\" + dir[i].Name;
                 GetMultiFile(pathNode, isFullName);
             }

             // loop through files
             for (int j = 0; j < filecount; j++)
             {
                 if (isFullName)
                 {
                     fileList.Add(file[j].FullName);
                 }
                 else
                 {
                     fileList.Add(file[j].Name);
                 }
             }

             return fileList. ToArray();
         }
         catch (Exception ex)
         {
             Console.WriteLine (ex.Message + "\r\nThe location of the error is: Form1.PaintTreeView()");
         }

         return null;
     }

     private static bool GetMultiFile(string path, bool isFullName = false)
     {
         if (Directory. Exists(path) == false)
         { return false; }

         DirectoryInfo dirs = new DirectoryInfo(path); //Get the directory object of the path where the program is located
         DirectoryInfo[] dir = dirs.GetDirectories();//Get the folder object under the directory
         FileInfo[] file = dirs.GetFiles();//Get the file object under the directory
         int dircount = dir.Count();//Get the number of folder objects
         int filecount = file.Count();//Get the number of file objects
         int sumcount = dircount + filecount;

         if (sumcount == 0)
         { return false; }

         // loop through folders
         for (int j = 0; j < dircount; j++)
         {
             string pathNodeB = path + "\\" + dir[j].Name;
             GetMultiFile(pathNodeB, isFullName);
         }

         // loop through files
         for (int j = 0; j < filecount; j++)
         {
             if (isFullName)
             {
                 fileList.Add(file[j].FullName);
             }
             else
             {
                 fileList.Add(file[j].Name);
             }
         }
         return true;
     }

     /// <summary>
     /// Copy the file multiple times for a large file true: the copy is successful false: the copy fails
     /// </summary>
     /// <param name="soucrePath">Original file path</param>
     /// <param name="targetPath">Copy target file path</param>
     /// <returns></returns>
     public static bool CopyFile(string sourcePath, string targetPath)
     {
         try
         {
             // read the copied file stream
             using (FileStream fsRead = new FileStream(soucrePath, FileMode. Open, FileAccess. Read))
             {
                 //write to file copy stream
                 using (FileStream fsWrite = new FileStream(targetPath, FileMode. OpenOrCreate, FileAccess. Write))
                 {
                     byte[] buffer = new byte[1024 * 1024 * 2]; // read 2M each time
                     while (true)//The file is large, read 2M each time
                     {
                         int n = fsRead.Read(buffer, 0, buffer.Count());//Data read each time n: is the actual data size read each time
                         //If n=0, it means that the read data is empty and has been read to the end, jump out of the loop
                         if (n == 0)
                         {
                             break;
                         }
                         //write each time the actual data size read
                         fsWrite.Write(buffer, 0, n);
                     }
                 }
             }
             return true;
         }
         catch (Exception ex)
         {
             return false;
         }
     }
     #endregion
 }

相关问题