代码从VB转换为C# [英] code convert from VB to C#

查看:172
本文介绍了代码从VB转换为C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是c新手#



我想将代码转换为c#来自VB net。



我试图将下面的代码转换为使用vb转换为c#转换工具,但转换后的代码无效。



Hi,

I am new to c#

I want to convert below code to c# from VB net.

I tried to convert below code using vb to c# converter tool but converted code is not working.

If FileUpload1.HasFile Then
                Dim MyPath As String = "C:\Uploads\" & Session("varFTRID")
                Dim MyName As String = Dir(MyPath, vbDirectory)
                If MyName = "" Then 'No folder for this SR
                    MkDir(MyPath)
                End If
                Try
                    FileUpload1.SaveAs(MyPath & "\" & FileUpload1.FileName)
                Catch ex As Exception
                    lblPauseWarning.Text = "ERROR: " & ex.Message.ToString()
                End Try
            End If

推荐答案

C#表示法中的实际代码是这样的,



The actual code in the C# notation would be this way,

if (FileUpload1.HasFile) {
	string MyPath = "C:\\Uploads\\" + Session["varFTRID"];
    // If no directory exists
	if (!Directory.Exists(MyPath)) {
        // Create directory
		Directory.CreateDirectory(MyPath);
	}
    // Remaining code would be like this, 
	try {
		FileUpload1.SaveAs(MyPath + "\\" + FileUpload1.FileName);
	} catch (Exception ex) {
		lblPauseWarning.Text = "ERROR: " + ex.Message.ToString();
	}
}





它是从Telerik转换器转换而来,做了一些改动,似乎合法。你还有错误吗?



已做出以下更改。



更改了用于的命名空间System.IO。您需要使用此代码对其进行引用





It was converted from Telerik converter, made a few changes and seems legit. Do you still get error?

Following changes have been made.

Changed the namespace being used to System.IO. You need to make a reference to it, using this code

using System.IO; // also use the References and add a reference to System.IO assembly





Donot获取FileSystem.Dir,直接在代码中使用MyPath, FileSystem.Dir() [ ^ ]没用。



使用System.IO(C#)命名空间来处理目录。我相信,这是真实的错误,导致了问题;你需要引用所需的 Microsoft.VisualBasic.FileIO.FileSystem


这篇关于代码从VB转换为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆