如何在使用Visual Studio构建项目时自动将项目的DLL文件发布到文件夹中。

huangapple go评论117阅读模式
英文:

How to publish the project DLLs to a folder automatically when building the project in Visual Studio

问题

我有多个模块在我的应用程序中,我想动态地将模块的程序集复制到主启动项目的 bin 文件夹中,而不将其添加为引用。

我可以手动使用 Visual Studio 中的“发布”选项发布项目的 DLL。但我希望能够在构建模块项目时自动将模块的程序集复制到主启动项目的 bin 文件夹中。

除了使用 PostBuild 事件之外,是否有其他选项?

如果您能分享您的想法,将会非常有帮助。

英文:

I am having multiple modules in my application and I want to copy the module's assembly to the Main startup project's bin folder dynamically without adding as a reference.

I am able to manually publish the project's DLLs using the Publish option in the Visual Studio. But I want to be able to copy the module's assembly automatically to the main startup project bin folder when we builds the module project.

Is there any option other than using the PostBuild event?

It would be much helpful if you could you share your ideas.

答案1

得分: 1

不确定是否有更加优雅的方法来使用dotnet,但你总是可以在Windows上创建一个批处理*.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脚本文件等等,但总体思路是使用脚本。你可以查找一些批处理命令Bash命令的指南。这可能是一种老式但始终有效的方法。

英文:

Not sure if there is more elegant way for dotnet, but you always can create something as Batch *.bat file on Windows (or Bash script in Linux) and write any commands you want, like so:

dotnet build &lt;your_main_project_name&gt; 
dotnet publish &lt;your_second_project_name&gt; 
copy &lt;path_to_second_bin&gt; &lt;path_to_target_bin&gt; 

Of cource, there are also special PowerShell script files and so on, but the general idea is to use scripts. You may look for guidelines or check some Batch commands and Bash commands. It may be old-school, but always-working way

答案2

得分: 1

以下是代码的翻译部分:

你可以编写一个控制台程序来将程序集复制到主启动项目的 bin 文件夹中,但不幸的是,这仍然使用 PostBuild

以下是控制台程序的代码:

public class Program
{
    public static void Main(string[] args)
    {
        //string para1 = @&quot;D:\word_code\Server\myProj\\bin,casereport,D:\Web_zy\Bin&quot;;// 后跟源目录、dll 文件关键字、目标目录
        string para1 = args[0];
        string[] ar = para1.Split(',', '|', ',');//源项目 bin 路径、移动 dll 文件关键字、目标服务器路径
        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] 按任意键退出...");
        Console.ReadKey(true);
    }

    // 在一个目录中查找包含子目录的所有文件
    // 具体翻译不详,因为代码较长,但你可以根据上下文理解其功能。
    // 如果需要进一步的翻译,请提供具体的部分。
}

请注意,代码中的注释部分没有提供完整的翻译,因为涉及到详细的功能描述。如果你需要进一步的翻译或解释,请提供具体的注释部分。

英文:

You could write a console program to be able to copy the assembly to the main startup project bin folder, but unfortunately this still uses PostBuild.

The console program is as follows:

 public class Program
 {
     public static void Main(string[] args)
     {
         //string para1 = @&quot;D:\word_code\Server\myProj\\bin,casereport,D:\Web_zy\Bin&quot;;// followed by source, dll file keywords, target directory
         string para1 =args[0];
         string[] ar = para1.Split(&#39;,&#39;, &#39;|&#39;, &#39;,&#39;);//source project bin path, keyword of mobile dll file, target server path
         Console.WriteLine(&quot;Source: &quot; + ar[0]);
         Console.WriteLine(&quot;Target: &quot; + ar[2]);
         var moveFile = new List&lt;string&gt;();
         if (!string.IsNullOrWhiteSpace(ar[0]) &amp;&amp; !string.IsNullOrWhiteSpace(ar[1]))
         {
             var list=GetFiles(ar[0],&quot;&quot;, 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, $@&quot;{ar[2]}\{Path.GetFileName(item)}&quot;);
             }
         }
         Console.ForegroundColor = ConsoleColor.Green;
         Console.Write(&quot;[Success] Press any key to exit...&quot;);
         Console. ReadKey(true);
     }


     #region Find all files in a directory that contains subdirectories
     /// &lt;summary&gt;
     /// Get all files in the directory or the specified file type file address
     /// &lt;/summary&gt;
     public static List&lt;string&gt; fileList = new List&lt;string&gt;();
     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 &lt; dircount; i++)
             {
                 string pathNode = fullPath + &quot;\\&quot; + dir[i].Name;
                 GetMultiFile(pathNode, isFullName);
             }

             // loop through files
             for (int j = 0; j &lt; 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 + &quot;\r\nThe location of the error is: Form1.PaintTreeView()&quot;);
         }

         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 &lt; dircount; j++)
         {
             string pathNodeB = path + &quot;\\&quot; + dir[j].Name;
             GetMultiFile(pathNodeB, isFullName);
         }

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

     /// &lt;summary&gt;
     /// Copy the file multiple times for a large file true: the copy is successful false: the copy fails
     /// &lt;/summary&gt;
     /// &lt;param name=&quot;soucrePath&quot;&gt;Original file path&lt;/param&gt;
     /// &lt;param name=&quot;targetPath&quot;&gt;Copy target file path&lt;/param&gt;
     /// &lt;returns&gt;&lt;/returns&gt;
     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
 }

huangapple
  • 本文由 发表于 2023年8月10日 19:59:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定