将VB代码转换为C#

问题描述:

我一直在努力理解这个VB代码,我试图建立一个使用蓝牙连接进行通信的应用程序,任何人都可以请帮助我将ti转换为C#,以便我能理解它正常。将VB代码转换为C#

' The host part of the URI is the device address, e.g. IrDAAddress.ToString(), 
' and the file part is the OBEX object name. 
Dim addr As String = "112233445566" 
Dim uri As New Uri("obex://" & addr & "/HelloWorld.txt") 
Dim req As New ObexWebRequest(uri) 
req.ReadFile("Hello World.txt") 
Dim rsp As ObexWebResponse = CType(req.GetResponse(),ObexWebResponse) 
Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode) 



    ' The host part of the URI is the device address, e.g. IrDAAddress.ToString(), 
' and the file part is the OBEX object name. 
Dim addr As String = "112233445566" 
Dim uri As New Uri("obex://" & addr & "/HelloWorld2.txt") 
Dim req As New ObexWebRequest(uri) 
Using content As Stream = req.GetRequestStream() 
    ' Using a StreamWriter to write text to the stream... 
    Using wtr As New StreamWriter(content) 
     wtr.WriteLine("Hello World GetRequestStream") 
     wtr.WriteLine("Hello World GetRequestStream 2") 
     wtr.Flush() 
     ' Set the Length header value 
     req.ContentLength = content.Length 
    End Using 
    ' In this case closing the StreamWriter also closed the Stream, but ... 
End Using 
Dim rsp As ObexWebResponse = CType(req.GetResponse(),ObexWebResponse) 
Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode) 



    Dim lsnr As New ObexListener(ObexTransport. Bluetooth) 
lsnr.Start() 
' For each connection 
Dim ctx As ObexListenerContext = lsnr.GetContext() 
Dim req As ObexListenerRequest = ctx.Request 
Dim pathSplits() As String = req.RawUrl.Split('/') 
Dim filename As String = pathSplits(pathSplits.Length – 1) 
req.WriteFile(filename) 
' 
lsnr.Stop() 




    Dim addr As BluetoothAddress _ 
    = BluetoothAddress.Parse("001122334455") 
' 
Dim ep As New BluetoothEndPoint(addr, BluetoothService.SerialPort) 
Dim cli As New BluetoothClient 
cli.Connect(ep) 
Dim peerStream As Stream = cli.GetStream() 
peerStream.Write/Read ... 

提前感谢!

+2

您是否尝试过使用在线转换器? – sloth 2013-04-11 09:28:47

+0

你有什么尝试?也许看看[这](http://stackoverflow.com/questions/2910022/bluetooth-file-send) – 2013-04-11 09:29:36

+0

有没有你不明白的任何特定部分?在那里似乎没有任何花哨的东西...... – sloth 2013-04-11 09:29:55

只需粘贴到您的VB.NET代码中,这个免费的实用程序会自动将其转换为C#中的等价物。

只需经过VB.Net to C#

或任何其他在线译码器。 VB.Net -> C#

希望它能帮助你。