如何将Visual Basic的Windows Forms App迁移至Windows Forms App (.NET Framework)
将Windows Forms App(.NET Core/.NET 5+)迁移为Windows Forms App (.NET Framework)(Visual Basic)
不用重新创建项目,通过修改配置和代码适配就能完成迁移,具体步骤如下:
1. 先备份项目
把整个项目文件夹复制一份到安全位置,避免操作失误丢失代码。
2. 修改项目文件(.vbproj)
- 关闭Visual Studio,找到项目的
.vbproj文件,用记事本或VS Code打开。 - 直接把整个XML内容替换为下面的.NET Framework VB WinForms项目模板结构:
<?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>{替换成原项目的GUID}</ProjectGuid> <OutputType>WinExe</OutputType> <RootNamespace>你的项目命名空间</RootNamespace> <AssemblyName>你的程序集名称</AssemblyName> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion> <!-- 可选择4.7.2等其他版本 --> <FileAlignment>512</FileAlignment> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <Deterministic>true</Deterministic> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <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' "> <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.Data" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Drawing" /> <Reference Include="System.Windows.Forms" /> <Reference Include="System.Xml" /> <Reference Include="System.Xml.Linq" /> </ItemGroup> <ItemGroup> <Compile Include="**\*.vb" /> <EmbeddedResource Include="**\*.resx" /> </ItemGroup> <ItemGroup> <None Include="App.config" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" /> </Project>- 替换
{替换成原项目的GUID}:从原项目文件中复制<ProjectGuid>的值粘贴过来即可; - 将
你的项目命名空间和你的程序集名称替换为原项目对应的内容; TargetFrameworkVersion可根据需求选择合适的.NET Framework版本,v4.8是目前常用的稳定版本。
- 替换
3. 添加App.config文件
在项目根目录新建App.config文件,内容如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> </startup> </configuration>
4. 调整数据库相关引用和代码
- 如果原项目使用了EF Core等.NET Core专属数据库库,需替换为.NET Framework兼容版本(比如EF6);
- 在App.config的
<configuration>节点内添加数据库连接字符串:<connectionStrings> <add name="MyDbConnection" connectionString="Data Source=.;Initial Catalog=你的数据库名;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> - 代码中若使用了.NET Core专属API(比如
HttpClient的新特性),需调整为.NET Framework兼容的写法。
5. 重新加载项目并验证
- 打开Visual Studio,加载修改后的项目;
- 清理项目后重新生成,逐一修复编译错误(如缺失引用、API差异);
- 测试所有原有功能,确保数据库操作正常运行。
注意事项
- 如果原项目大量使用.NET Core/.NET 5+专属功能(如顶级语句、跨平台API),迁移时需逐个适配;
- 第三方NuGet包需确认存在.NET Framework兼容版本,无兼容版则需寻找替代方案。
内容的提问来源于stack exchange,提问作者Juan Arias
相关产品推荐
相关产品推荐

