Create folder inside debug or release con console

2019-05-28 22:00发布

i have a console application in vs2010 (C#) and in the project, i have a Folder added by me (right click on project.. add->folder) and i want that when i compile the application (debug or release), then the folder will be created (if not exists) in the debug or release directory.

Is that possible?

The console application is a daemon that access to a database and send emails with templates allocated in that folder.

I hope you can help me. Thanks!

2条回答
Melony?
2楼-- · 2019-05-28 22:44

There's no "automatic" way to get VS to create folders (other than the specified output folder) during a build, but there's two pretty easys ways to accomplish it.

  • 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.

查看更多
仙女界的扛把子
3楼-- · 2019-05-28 22:48

I solve it, like this: in the 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>

Thank you for your help!

查看更多
登录 后发表回答