2013-04-11 116 views
-2

我一直在努力理解這個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

回答

3

只需粘貼到您的VB.NET代碼中,這個免費的實用程序會自動將其轉換爲C#中的等價物。

只需經過VB.Net to C#

或任何其他在線譯碼器。 VB.Net -> C#

希望它能幫助你。