从VB.Net转换时C#代码出错

本文关键字:代码 出错 转换 VB Net | 更新日期: 2023-09-27 17:58:16

我从VB.Net转换了以下代码:

Private Sub UVTapHuanLuyen_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    txtPath.Text = strPath & "Dulieu'"
    Dim di As New DirectoryInfo(txtPath.Text)
    Dim tenfile, FS() As FileInfo
    FS = di.GetFiles("*.txt")
    For Each tenfile In FS
        lstPath.Items.Add(tenfile.FullName)
    Next
    lblSoLuong.Text = lstPath.Items.Count
    rd_ThuSpam.Checked = True
End Sub

在生成的c#代码中,txtPath.Text = strPath & "Dulieu'"出现错误我提前声明了strPath对象,但仍然再次收到错误。这是C#中的代码:

 private void UV_Trains_Load(object sender, EventArgs e)
    {
        txt_Path.Text = strPath + "Dulieu''";
        DirectoryInfo di = new DirectoryInfo(txt_Path.Text);
        FileInfo tenfile = default(FileInfo);
        FileInfo[] FS = null;
        FS = di.GetFiles("*.txt");
        foreach (FileInfo element in FS) 
        {
            tenfile = element;
            lst_Path.Items.Add(txt_Path.Text);
        }
        lbl_Soluong.Text = lst_Path.Items.Count.ToString();
    }

从VB.Net转换时C#代码出错

在C#中,"Dulieu'"中的斜线表示最后一个引号是字符串的一部分。要修复它,可以使用双斜杠。

txtPath.Text = strPath + "Dulieu''"

或者,您可以在字符串文字前面加上@符号,将其标记为逐字逐句的字符串。

txtPath.Text = strPath + @"Dulieu'"

&替换为+
在vb.net中,&是一个字符串串联运算符,在c#中,正确的运算符是+