How do I convert a C# Windows Forms App project to a C# Windows Forms App (.NET Framework) project in Visual Studio?

Zack T 6 Reputation points
2021-08-12T19:36:39.487+00:00

Hey Everyone,

I need some help and I am hoping you know how to do it. I am currently in school and I initially created my entire C# final project using a "Windows Forms App" project template in Visual Studio 2019 and have done all my work under that so far. However, I now need to add a database into my program and connect to it. However, to be able to use the .NET framework to create and link a database to my application, I have to be using the "Windows Forms App (.NET Framework) project template. Do you know how I can convert my current project from regular Windows Forms App to the Windows Forms App(.Net Framework) version so I can start incorporating the database? I really can't afford to start this entire project from scratch by creating a new Windows Forms App(.NET Framework) project.

See the image below to understand what I mean. I made the program using the template in the red square but I need it to be the one in the green square.

122891-apptypes.png

Thank you very much for your help!

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,857 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Michael Taylor 49,246 Reputation points
    2021-08-12T20:15:43.41+00:00

    Firstly you do not need to convert your project to use SQL (or any other database). Using Manage NuGet Packages for Solution search for System.Data.SqlClient and add that package as a reference to your project(s). You can then use the same code that works for .NET Framework.

    If you really want to convert to .NET Framework then select the project file in Solution Explorer to open it up in the editor. Find the line that says <TargetFramework></TargetFramework>. Change the contents of the element from whatever it is (e.g. netcoreapp1.0) to net472.

    <TargetFramework>net472</TargetFramework>
    

    Close the file and VS should reload the project. If it reports a warning or fails to reload then restart VS. You are now targeting .NET Framework. There will be some differences but probably nothing you'll notice. The biggest difference will be that if you had an appsettings.json file then it won't be used anymore. Instead you'll need an app.config. You should be able to use the Add New Item option on the project to add a new Application Configuration file to get it to auto-generate one for you. Your connection string will then go in there.

    0 comments No comments

  2. Karen Payne MVP 35,201 Reputation points
    2021-08-13T09:47:16.483+00:00

    Going with @Michael Taylor recommendation, here is a sample project showing how to read a connection string for a database, in this case SQL-Server but can work for any database with minor changes.

    Make sure to read readme file before anything.

    This is the code to connect

    public static (bool success, Exception exception) TestConnection()  
    {  
        try  
        {  
            using var cn = new SqlConnection() {ConnectionString = ConnectionString()};  
            cn.Open();  
            return (true, null);  
        }  
        catch (Exception exception)  
        {  
            return (false, exception);  
        }  
    }  
    

    Code to read appsettings.json

    public class Helper  
    {  
        public static string ConnectionString()  
        {  
            InitConfiguration();  
            return InitOptions<DatabaseSettings>("database").ConnectionString;  
        }  
        private static IConfigurationRoot InitConfiguration()  
        {  
            var builder = new ConfigurationBuilder()  
                .SetBasePath(Directory.GetCurrentDirectory())  
                .AddJsonFile("appsettings.json");  
              
            return builder.Build();  
        }  
        public static T InitOptions<T>(string section) where T : new()  
        {  
            IConfigurationRoot config = InitConfiguration();  
            return config.GetSection(section).Get<T>();  
        }  
    }  
    

    appsettings.json

    {  
      "database": {  
        "DatabaseServer": ".\\SQLEXPRESS",  
        "Catalog": "School",  
        "IntegratedSecurity": "true",  
        "UsingLogging": "true",  
        "Timeout": 5   
      }  
    }  
    
    0 comments No comments

  3. zawier 1 Reputation point
    2022-02-26T00:48:07.583+00:00

    Ok, Guys, I have exactly the same question. How to migrate Windows Forms into Windows Forms Framework? I would like to achieve pure framework.

    I know you can change for that purpose :

    > <TargetFramework>net472</TargetFramework>
    

    But it wont be exactly the same. From my point of view there is huge difference inside .csproject.
    For example Windows Forms declaration:

    > <Project Sdk="Microsoft.NET.Sdk">
    
    > 
    
    >   <PropertyGroup>
    
    >     <OutputType>WinExe</OutputType>
    
    >     <TargetFramework>net48</TargetFramework>
    
    >     <UseWindowsForms>true</UseWindowsForms>
    
    >   </PropertyGroup>
    
    > 
    
    >   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    
    >     <PlatformTarget>x64</PlatformTarget>
    
    >   </PropertyGroup>
    
    > 
    
    >   <ItemGroup>
    
    >     <PackageReference Include="FontAwesome.Sharp" Version="5.15.4" />
    
    >   </ItemGroup>
    
    > 
    
    >   <ItemGroup>
    
    >     <Reference Include="Microsoft.CSharp" />
    
    >     <Reference Include="System.Deployment" />
    
    >     <Reference Include="System.Windows.Forms" />
    
    >   </ItemGroup>
    
    > 
    
    >   <ItemGroup>
    
    >     <Compile Update="Forms\Release-PDF.cs">
    
    >       <SubType>Form</SubType>
    
    >     </Compile>
    
    >     <Compile Update="Forms\Procesy-RM.cs">
    
    >       <SubType>Form</SubType>
    
    >     </Compile>
    
    >     <Compile Update="Forms\Release-RestApi.cs">
    
    >       <SubType>Form</SubType>
    
    >     </Compile>
    
    >     <Compile Update="Forms\Release-WS.cs">
    
    >       <SubType>Form</SubType>
    
    >     </Compile>
    
    >     <Compile Update="Forms\Release-WWW.cs">
    
    >       <SubType>Form</SubType>
    
    >     </Compile>
    
    >     <Compile Update="Resource.Designer.cs">
    
    >       <DesignTime>True</DesignTime>
    
    >       <AutoGen>True</AutoGen>
    
    >       <DependentUpon>Resource.resx</DependentUpon>
    
    >     </Compile>
    
    >   </ItemGroup>
    
    > 
    
    >   <ItemGroup>
    
    >     <EmbeddedResource Update="Resource.resx">
    
    >       <Generator>ResXFileCodeGenerator</Generator>
    
    >       <LastGenOutput>Resource.Designer.cs</LastGenOutput>
    
    >     </EmbeddedResource>
    
    >   </ItemGroup>
    
    > 
    
    > </Project>
    

    But in the same time Windows Forms Framework will be presenting like that:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{BF10BA40-0932-49C8-A7F8-721010DC32D7}</ProjectGuid>
        <OutputType>WinExe</OutputType>
        <RootNamespace>Topola1</RootNamespace>
        <AssemblyName>Topola1</AssemblyName>
        <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
        <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
        <Deterministic>true</Deterministic>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <PlatformTarget>AnyCPU</PlatformTarget>
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Debug\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <PlatformTarget>AnyCPU</PlatformTarget>
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Core" />
        <Reference Include="System.Xml.Linq" />
        <Reference Include="System.Data.DataSetExtensions" />
        <Reference Include="Microsoft.CSharp" />
        <Reference Include="System.Data" />
        <Reference Include="System.Deployment" />
        <Reference Include="System.Drawing" />
        <Reference Include="System.Net.Http" />
        <Reference Include="System.Windows.Forms" />
        <Reference Include="System.Xml" />
      </ItemGroup>
      <ItemGroup>
        <Compile Include="Form1.cs">
          <SubType>Form</SubType>
        </Compile>
        <Compile Include="Form1.Designer.cs">
          <DependentUpon>Form1.cs</DependentUpon>
        </Compile>
        <Compile Include="MTRadioButton.cs">
          <SubType>Component</SubType>
        </Compile>
        <Compile Include="Program.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
        <EmbeddedResource Include="Form1.resx">
          <DependentUpon>Form1.cs</DependentUpon>
        </EmbeddedResource>
        <EmbeddedResource Include="Properties\Resources.resx">
          <Generator>ResXFileCodeGenerator</Generator>
          <LastGenOutput>Resources.Designer.cs</LastGenOutput>
          <SubType>Designer</SubType>
        </EmbeddedResource>
        <Compile Include="Properties\Resources.Designer.cs">
          <AutoGen>True</AutoGen>
          <DependentUpon>Resources.resx</DependentUpon>
        </Compile>
        <None Include="Properties\Settings.settings">
          <Generator>SettingsSingleFileGenerator</Generator>
          <LastGenOutput>Settings.Designer.cs</LastGenOutput>
        </None>
        <Compile Include="Properties\Settings.Designer.cs">
          <AutoGen>True</AutoGen>
          <DependentUpon>Settings.settings</DependentUpon>
          <DesignTimeSharedInput>True</DesignTimeSharedInput>
        </Compile>
      </ItemGroup>
      <ItemGroup>
        <None Include="App.config" />
      </ItemGroup>
      <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    </Project>
    

    It's a huge difference and as you see above.
    Please let's not focus why I want to convert it into old good framework (I have my reasons where newer version just faults and the same code in framework just works..).
    So please sitck with migrating approach.
    Thank you in advance.


  4. Osamah Hadi 0 Reputation points
    2023-06-03T10:54:01.6366667+00:00

    Mr ZackT Did you find a solution for yor problem I have the same problem

    0 comments No comments