在控制台应用程序的debug或release中创建文件夹

3
我在VS2010中有一个控制台应用程序(C#),在项目中我添加了一个文件夹(右键单击项目.. 添加->文件夹),我希望当我编译应用程序(调试或发布)时,文件夹会在调试或发布目录中创建(如果不存在)。
这是否可能?
该控制台应用程序是一个访问数据库并发送电子邮件的守护进程,其中模板分配在该文件夹中。
希望我能帮到你。谢谢!
2个回答

8

在构建期间,除了指定的输出文件夹外,VS没有“自动”创建文件夹的方式。但有两种相当简单的方法可以实现。

  • Use a post-build event, which you set up in the Build Events tab of your project's properties. This is basically a batch file that you run after the build completes, something like this:

    IF NOT EXIST $(OutDir)MySubFolder MKDIR $(OutDir)MySubFolder
    XCOPY /D $(ProjectDir)MySubFolder\*.tmpl $(OutDir)MySubFolder
    
  • Use MSBuild's AfterBuild event. This is my preferred method, mostly because it integrates better with our automated build process, but it's a little more involved:

    1. Right-click on your project node and Unload it
    2. Right-click on the unloaded project node and Edit the file
    3. Near the bottom is a commented-out pair of XML nodes. Uncomment the AfterBuild target and replace it with something like this:

      <Target Name="AfterBuild">
          <MakeDir Directory="$(OutDir)MySubFolder" Condition="!Exists('$(OutDir)MySubFolder')" />
      
          <CreateItem Include="$(ProjectDir)MySubFolder\*.tmpl">
            <Output TaskParameter="Include" ItemName="Templates" />
          </CreateItem>    
      
          <Copy SourceFiles="@Templates" DestinationFolder="$(OutDir)MySubFolder" ContinueOnError="True" />
      </Target>
      
    4. Save the changes, close the .csproj file, then right-click and Reload the project.


哇!太棒了!唯一的问题是在发布文件夹(或调试)中,文件夹被创建但里面没有复制任何内容,在发布文件夹中出现一个包含发布文件夹的 bin 目录。 - Phoenix_uy

5
我这样解决它: 在csproj文件中:
<Target Name="AfterBuild">
    <MakeDir Directories="$(OutDir)EmailTemplates" Condition="!Exists('$(OutDir)EmailTemplates')" />
    <ItemGroup>
      <Templates Include="$(ProjectDir)EmailTemplates\*.*" />
    </ItemGroup>
    <Copy SourceFiles="@(Templates)" DestinationFolder="$(OutDir)EmailTemplates" />
  </Target>

谢谢您的帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接