Visual Studio: How to “Copy to Output Directory” without copying the folder structure?

后端 未结 8 996
礼貌的吻别
礼貌的吻别 2020-12-04 08:43

I have a few dll files in \\lib folder of my project folder. In the property page of dll, I have selected \"Build Action\" as \"Content\" and \"Copy to Output Directory\" as

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 09:15

    An alternate method is just to leave the items as type None. In the solution explorer, click on the ones you want to deploy and set the Content property to True.

    Note: I did this in VS2019, and things can change from version to version.

    To get this to work, now right-click on your project, and select "Unload Project". Then right-click on the unloaded project and select "Edit project_name.vcxproj".

    In the editor, go all the way to the bottom of the file and insert this target right right before the trailing tag:

      
        
      
    

    Now right click on the unloaded project and select "Reload Project". Select to save and close if you are prompted.

    I also set the OutputDirectory to:

    $(SolutionDir)bin\$(Configuration)\$(Platform)\

    and the IntermediateDirectory to:

    $(SolutionDir)obj\$(Configuration)\$(ProjectName)\$(Platform)\

    in the Project Properties General page. This puts the output in a "bin" folder, and the intermediates in an "obj" folder in the root of your solution.

    Note: The $(SolutionDir) is not defined when you run MSBuild from the command line. There is a trick you can use to define that to the folder where the .sln file lives using GetDirectoryNameOfFileAbove. (left as an exercise for the reader). Also, it looks like in 2019 they are handling this correctly on the command line anyway. Yeah :) The $(SolutionDir) contains a trailing backslash, hence none after it. The results of each must have a trailing backslash.

    Now, if you own Pro or above, please don't do this every time you need to create a project. That would be lame. Instead, once you have your project setup just the way you like it, select Project -> Export Template. You give it a name, and the next time you want to create a project just like that one, just choose that name in the New Project dialog. (In older version, I think this was Files -> Export Teamplate....)

提交回复
热议问题