英文:
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 <your_main_project_name>
dotnet publish <your_second_project_name>
copy <path_to_second_bin> <path_to_target_bin>
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 = @"D:\word_code\Server\myProj\\bin,casereport,D:\Web_zy\Bin";// 后跟源目录、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 = @"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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论