如何使用 C# 以编程方式创建 VC++ 项目?

smw*_*dia 5 visual-studio visual-studio-2013

我想用 C# 创建一个 VC++ 项目。我看到这篇关于创建C# and VB console application projects. 但是VC++项目怎么办呢?

添加 1

我遇到了与以下线程完全相同的问题。而它的解决方案是操作原始 XML,这很悲惨......有没有我可以使用的某种 API?

如何以编程方式创建空的 Visual C++ 项目?

添加 2

除了创建一个.vcxprojfie。我想以编程方式创建一个解决方案.sln文件。因为我的代码库被分成了很多项目。

smw*_*dia 1

好的。我通过直接操作*.sln*.vcxproj文件解决了这个问题。

细节:

对于*.vcxproj文件,我创建了以下模板:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <ItemGroup>
    __SM_C_FILES__
  </ItemGroup>
  <ItemGroup>
    __SM_H_FILES__
  </ItemGroup>
  <PropertyGroup Label="Globals">
    <ProjectGuid>__SM_PROJECT_GUID__</ProjectGuid>
    <RootNamespace>__SM_PROJECT_ROOTNS__</RootNamespace>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    <UseDebugLibraries>true</UseDebugLibraries>
    <PlatformToolset>v120</PlatformToolset>
    <CharacterSet>MultiByte</CharacterSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    <UseDebugLibraries>false</UseDebugLibraries>
    <PlatformToolset>v120</PlatformToolset>
    <WholeProgramOptimization>true</WholeProgramOptimization>
    <CharacterSet>MultiByte</CharacterSet>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ImportGroup Label="ExtensionSettings">
  </ImportGroup>
  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup />
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <ClCompile>
      <WarningLevel>Level3</WarningLevel>
      <Optimization>Disabled</Optimization>
      <SDLCheck>true</SDLCheck>
      <AdditionalIncludeDirectories>__SM_ADDITIONAL_INCLUDE_DIRS_DEBUG__</AdditionalIncludeDirectories>
    </ClCompile>
    <Link>
      <GenerateDebugInformation>true</GenerateDebugInformation>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <ClCompile>
      <WarningLevel>Level3</WarningLevel>
      <Optimization>MaxSpeed</Optimization>
      <FunctionLevelLinking>true</FunctionLevelLinking>
      <IntrinsicFunctions>true</IntrinsicFunctions>
      <SDLCheck>true</SDLCheck>
      <AdditionalIncludeDirectories>__SM_ADDITIONAL_INCLUDE_DIRS_RELEASE__</AdditionalIncludeDirectories>
    </ClCompile>
    <Link>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <EnableCOMDATFolding>true</EnableCOMDATFolding>
      <OptimizeReferences>true</OptimizeReferences>
    </Link>
  </ItemDefinitionGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
  </ImportGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

包含子__SM_C_FILES__模板如下:

<ClCompile Include="__SM_C_FILE__" />
Run Code Online (Sandbox Code Playgroud)

包含子__SM_H_FILES__模板如下:

<ClInclude Include="__SM_H_FILE__" />
Run Code Online (Sandbox Code Playgroud)

对于*.sln文件,我创建了一个模板,如下所示:

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.40629.0
MinimumVisualStudioVersion = 10.0.40219.1

Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "__SM_PROJ_NAME__", "__SM_PROJ_FILE_PATH__", "__SM_PROJ_GUID__"
EndProject

Global
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Win32 = Debug|Win32
        Release|Win32 = Release|Win32
    EndGlobalSection
    GlobalSection(ProjectConfigurationPlatforms) = postSolution

        {__SM_PROJ_GUID__}.Debug|Win32.ActiveCfg = Debug|Win32
        {__SM_PROJ_GUID__}.Debug|Win32.Build.0 = Debug|Win32
        {__SM_PROJ_GUID__}.Release|Win32.ActiveCfg = Release|Win32
        {__SM_PROJ_GUID__}.Release|Win32.Build.0 = Release|Win32

    EndGlobalSection
    GlobalSection(SolutionProperties) = preSolution
        HideSolutionNode = FALSE
    EndGlobalSection
EndGlobal
Run Code Online (Sandbox Code Playgroud)

请注意这些__SM_*__部分,我project-specific使用纯字符串替换将它们替换为内容,这非常简单。

并且在*.sln文件中,如果您有多个项目,则可能需要生成多个部分的节Project

{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}是一个众所周知的 GUID,代表VC++项目类型。可以在此处找到完整的列表。

因此,我避免了操作 XML。

与此类似的线程不同,我没有创建该*.vcxproj.filters文件。虽然它看起来有点难看,但我现在可以忍受。