简体   繁体   中英

Automatically Include generated file in Visual Studio Project

I'm writing an application that will enable the creation of SQL files within visual studio. The user will enter commands via the Package Manager console which will generate sql scrips and deposit them in a specific directory within the Visual Studio Project.

The problem I have is that, when the files are generated, they are present on the file system, but not in Visual Studio. This is expected of course, as I need to then go and actively include the files within Solution explorer, but this isn't what I want. I want the files to "Magically" appear in solution explorer immediately after they're generated.

I've seen various solutions to similar problems mostly featuring amendments to the .csproj file such as this

<Compile Include="Sql\**\*.sql" />

but this isn't what i'm looking for. What i'm after is similar to how, for example, Entity Framework or MvcScaffolding work, where files / folders just magically drop into the project when commands run in PMC. I'm aware this runs off T4 templating, but that seems like too complex a solution for a simple issue like this.

I should qualify that there's no voodoo going on in the creation of the files, just plain old File.Create() stuff.

I'm open to any suggestions.

Thanks.

Check out this answer for a solution that worked for me. I have the same use-case where code outputs flat files and I need to include this output in the project.

At the end of your .csproj file add the following:

<Target Name="BeforeBuild">
    <ItemGroup>
        <Content Include="Sql\**\*.sql" />
    </ItemGroup>
</Target>

IMHO, T4 is the way to go. You don't want to be bothering with older technologies for what you are trying to do.

Having said that, I wonder why is it required for the files to be added to the solution explorer? is it for source control purposes? (usually you don't want to source control auto generated files, you want to source control the original model). Note that you could always click the 'show all' button and the files will appear in the solution explorer, without actually being a part of the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM