C# 使用后期构建事件将文件从一个项目复制到另一个项目...VS2010
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11001822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy file(s) from one project to another using post build event...VS2010
提问by BoundForGlory
I have a solution with 3 projects in it. I need to copy a view from one project to another. I'm able to copy the created DLL via post build events like so:
我有一个包含 3 个项目的解决方案。我需要将视图从一个项目复制到另一个项目。我可以通过像这样的后期构建事件复制创建的 DLL:


So i want to copy the file in project one '/Views/ModuleHome/Index.cshtml' to a folder in project 2. How do I copy file(s) to my desired project via post-build event? Thanks
因此,我想将项目一“/Views/ModuleHome/Index.cshtml”中的文件复制到项目 2 中的文件夹中。如何通过构建后事件将文件复制到我想要的项目?谢谢
采纳答案by Darin Dimitrov
xcopy "$(ProjectDir)Views\Home\Index.cshtml" "$(SolutionDir)MEFMVCPOC\Views\Home"
and if you want to copy entire folders:
如果你想复制整个文件夹:
xcopy /E /Y "$(ProjectDir)Views" "$(SolutionDir)MEFMVCPOC\Views"
Update: here's the working version
更新:这是工作版本
xcopy "$(ProjectDir)Views\ModuleAHome\Index.cshtml" "$(SolutionDir)MEFMVCPOC\Views\ModuleAHome\" /Y /I
Here are some commonly used switches with xcopy:
以下是一些常用的开关xcopy:
- /I- treat as a directory if copying multiple files.
- /Q- Do not display the files being copied.
- /S- Copy subdirectories unless empty.
- /E- Copy empty subdirectories.
- /Y- Do not prompt for overwrite of existing files.
- /R- Overwrite read-only files.
- /I- 如果复制多个文件,则视为一个目录。
- /Q- 不显示正在复制的文件。
- /S- 除非为空,否则复制子目录。
- /E- 复制空子目录。
- /Y- 不提示覆盖现有文件。
- /R- 覆盖只读文件。
回答by swapneel
Call Batchfile which will run Xcopyfor required files source to destination
调用Batch文件,它将Xcopy为所需的文件源运行到目标
call "$(SolutionDir)scripts\copyifnewer.bat"
回答by Hallgeir Engen
xcopy "your-source-path" "your-destination-path" /D /y /s /r /exclude:path-to-txt- file\ExcludedFilesList.txt
Notice the quotes in source path and destination path, but not in path to exludelist txt file.
注意源路径和目标路径中的引号,但不在 exludelist txt 文件的路径中。
Content of ExcludedFilesList.txt is the following: .cs\
ExcludedFilesList.txt 的内容如下:.cs\
I'm using this command to copy file from one project in my solution, to another and excluding .cs files.
我正在使用此命令将文件从我的解决方案中的一个项目复制到另一个项目,但不包括 .cs 文件。
/D Copy only files that are modified in sourcepath
/y Suppresses prompting to confirm you want to overwrite an existing destination file.
/s Copies directories and subdirectories except empty ones.
/r Overwrites read-only files.
回答by Namrata Jain
xcopy "$(TargetDir)*$(TargetExt)" "$(SolutionDir)\Scripts\MigrationScripts\Library\" /F /R /Y /I
xcopy "$(TargetDir)*$(TargetExt)" "$(SolutionDir)\Scripts\MigrationScripts\Library\" /F /R /Y /I
/F – Displays full source & target file names
/F – 显示完整的源和目标文件名
/R – This will overwrite read-only files
/R - 这将覆盖只读文件
/Y – Suppresses prompting to overwrite an existing file(s)
/Y - 禁止提示覆盖现有文件
/I – Assumes that destination is directory (but must ends with )
/I – 假设目标是目录(但必须以 结尾)
A little trick – in target you must end with character \ to tell xcopy that target is directory and not file!
一个小技巧——在目标中你必须以字符 \ 结尾来告诉 xcopy 目标是目录而不是文件!
回答by kim
Like the previous replies, I'm also suggesting xcopy. However, I would like to add to Hallgeir Engen's answer with the /excludeparameter. There seems to be a bug with the parameter preventing it from working with path names that are long or that contain spaces, as quotes will not work. The path names need to be in the "DOS"-format with "Documents" translating to "DOCUME~1" (according to this source).
像之前的回复一样,我也建议xcopy. 但是,我想将/exclude参数添加到 Hallgeir Engen 的答案中。该参数似乎存在一个错误,阻止它使用长路径名或包含空格的路径名,因为引号不起作用。路径名需要采用“DOS”格式,“文档”转换为“DOCUME~1”(根据此来源)。
So, if you want to use the \exclude parameter, there is a workaround here:
所以,如果你想使用\排除参数,有一种变通方法在这里:
cd $(SolutionDir)
xcopy "source-relative-to-path-above" "destination-relative-to-path-above
/exclude:exclude-file-relative-path
Note that the source and destination paths can (and should, if they contain spaces) be within quotes, but notthe path to the exclude file.
请注意,源路径和目标路径可以(并且应该,如果它们包含空格)在引号内,而不是排除文件的路径。
回答by Super-Mantis
I use it like this.
我是这样用的。
xcopy "$(TargetDir)$(TargetName).dll" "$(SolutionDir)Lib\TIRM\x86\" /F /Y
xcopy "$(TargetDir)$(TargetName).lib" "$(SolutionDir)Lib\TIRM\x86\" /F /Y
/F : Copy source is File
/Y : Overwrite and don't ask me
Note the use of this. $(TargetDir) has already '\' "D:\MyProject\bin\" = $(TargetDir)
注意这个的使用。$(TargetDir) 已经 '\' "D:\MyProject\bin\" = $(TargetDir)
You can find macro in Command editor
您可以在命令编辑器中找到宏
回答by Flot2011
If you want to take into consideration the platform(x64, x86 etc) and the configuration(Debug or Release) it would be something like this:
如果你想考虑平台(x64、x86 等)和配置(调试或发布),它会是这样的:
xcopy "$(SolutionDir)$(Platform)$(Configuration)$(TargetName).dll" "$(SolutionDir)TestDirectory\bin$(Platform)$(Configuration)\" /F /Y
回答by Bizhan
This command works like a charm for me:
这个命令对我来说就像一个魅力:
for /r "$(SolutionDir)libraries" %%f in (*.dll, *.exe) do @xcopy "%%f" "$(TargetDir)"
It recursivelycopies every dlland exefile from MySolutionPath\librariesinto the bin\debugor bin\release.
它递归地将每个dll和exe文件复制MySolutionPath\libraries到bin\debugor 中bin\release。
You can find more info in here
你可以在这里找到更多信息

