36

有谁知道从 VB6 代码转换为 C# 的方法?

有没有工具可以为我做到这一点?

我可以遵循任何迁移过程来执行此操作吗?

4

10 回答 10

28

VisualStudio 提供(或至少提供)一个向导来进行从 VB6 到 VB.NET 的转换(然后可以通过一些工作将其转换为 C#,这可能得益于#develop 的 VB.NET <-> C# 转换器),但是当我最后一次使用它时,对于任何不平凡的事情都需要做很多手动工作,所以我怀疑如果这是一个大型和/或重要的应用程序,你可能会更好地手动重写或移植。

于 2008-10-07T17:54:28.827 回答
12

它可能会让人觉得有点厚颜无耻,但你的大脑可能是最好的工具。可能值得重写。

也许你不需要移植它。VB6可以变成COM组件吗?端口到 COM,从 C# 调用,及时返回 Judy 法官。

于 2008-10-07T19:52:20.323 回答
10

As someone who has done this a bunch of times, this is not an easy process. You can use the VB6 to VB.Net tool as stated in this answer, and then use either Reflector or SharpDevelop to convert to C#. With the SharpDevelop conversion, a few caveats. It screws up all the array references and thinks that they are function calls, and all the logical operators are converted to bitwise logical operators (And becomes & not &&). With Reflector you lose a bunch of stuff. Also the Visual Studio converter fails on a lot of large projects, just hangs and never completes.

Once you have got your code converted into C#, you have to start the real work. The conversion gets you at best 50% of the way there, you have to fix a ton of stuff (you will see your code littered with TODO's), refactor a ton of stuff, and at the end you are left with C# that is a representation of your VB6 -- unless you have very nice VB6 code not a place you really want to be. Also all of your code with be littered with the VB helpers rather than using proper DotNet functions (all the string functions are helpers rather than class objects, for examples0. If you used Variants at all those all have to be rewritten. If you used a lot of API calls, they tend to need rewritting.

In the end you will get a base, but converting a large project (20-30 forms, 30 classes, 30 modules) can take several man months. Rewritting from scratch, however, may take twice as long and you lose all of your business logic. So, it can be done (I have done it with 3 or 4 large projects), but there is no panacea, no silver bullet, and any tool that says it will do it for you alone, is lying.

于 2008-10-08T14:15:27.333 回答
8

使用新版本的 Visual Studio 打开您的项目,将您的代码转换为 VB.Net,然后下载.Net Reflector以帮助您进行 C# 转换。

祝你好运!

于 2008-10-07T18:05:02.847 回答
8

Artinsoft(现在更名为Mobilize.Net)就是这样做的,特别是Visual Basic Upgrade Companion

然而,即使在使用 VBUC 之后,系统的某些部分仍然需要手动迁移/校对。但它通常是原始问题的一小部分。由于过去的迁移经验,一些迁移​​问题已经得到解决。

Artinsoft 是建造 Visual Studio 附带的向导的同一家公司,在theraccoonbear 的帖子中提到。但是,如果我没记错的话,向导只会将 VB6 迁移到 VB.Net。

全面披露:我为 Artinsoft 工作

于 2008-10-07T19:25:03.857 回答
4

简短的回答是 VB6 和 VB.NET(以及因此 C#)是独立的语言,但相关的语言。两个平台之间存在许多细微的差异(Integer 是 Int32 而不是 Int16)和严重的差异(图形、表单和打印引擎)。您需要将此视为您正在转换到一个完全不同的平台。

于 2008-10-08T12:43:53.400 回答
3

我使用了 2 个免费的:

http://www.carlosag.net/Tools/CodeTranslator/

http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx

于 2008-10-07T20:43:06.677 回答
3

Microsoft 推荐ArtinSoft 的 Visual Basic Upgrade Companion (VBUC)

Microsoft 和 ArtinSoft 合作为您提供免费的 VBUC 许可证。下面的折扣代码可用于购买许可证,允许您免费将包含多达 10,000 行 VB6 代码的应用程序迁移到 VB.NET 或 C#。它也可以在购买更大的许可证时应用。

它并不完美,生成的代码在编译之前仍然需要大量手动修复,但这个工具仍然值得一试。

于 2012-12-26T06:15:58.717 回答
2

要问自己的一个问题是,拥有 C# 而不是 VB.NET 有多重要?如果您正在转换一个大型应用程序,也许您应该升级到 VB.NET,它会更便宜。

VB6 中有一些在 C# 中没有完全等效的特性(错误处理、后期绑定等)。问问自己为什么值得手动转换它们,当您可以转到支持它们的 VB.NET 时。

如果您不相信我(而且没有人投票支持我的答案,呜呜呜!),也许您会相信Francesco Balena和 Dan Appleman(在《转向 VB.NET 》一书中)同意我的观点。

于 2008-10-13T17:21:29.487 回答
1

在 GreatMigrations,我们是手工完成的——我们用我们的双手来开发一个 VB6/COM 编译器。编译器读取一个 VBP(或一组相关的 VBP)和引用的 COM 库并构造一个“语义模型”。该模型是一组极其详细的符号表和操作码流,可以完整准确地描述源系统定义的所有信息结构和执行的操作。

然后,该工具应用一系列算法来修改和重组模型,使其与 .NET 更兼容,并可选择应用自定义转换;例如用 .NET 类替换 COM 组件。转换规则是由人设计并“手工”创建的,以满足他们的独特要求。

最后,模型被“执行”,但不是分配数据模型和执行操作,而是以目标平台的符号编写文件结构、数据模型和操作。我们可以使用这种方法编写 VB.NET 和 C#。我们也使用这个过程将 ASP 网站迁移到 ASPX,(我们也有一个多方言 FORTRAN-to-C 产品)

该方法不会在第一次尝试时产生生产就绪的软件,但可以通过修改工具的配置并重新运行翻译过程来改进并制作“更好”的软件。我们工具的优势在于它的可重复性、速度和灵活性,这有助于敏捷的工具辅助重写方法。IMO,利用我们的方法的迁移团队将受益于分配更多资源来评估重新设计和开发过程改进的替代方案,而不是重新收集功能需求、手工编码和测试。他们最终将享受到一个更可维护的系统和迁移后更忠实地再现原始应用程序功能,以及迁移过程中更好的控制和可见性。

我们工具的试用版可在此处获得 gmStudio Trail 请求下载。它将允许您试验高达 10K LOC 的 ASP/VB 代码并运行多达 30 个会话。

于 2009-09-24T11:44:35.777 回答