iT邦幫忙

0

請問能教我這段CODE嗎 我需要將他轉成C#

Imports System.Management
Imports System.Data.SqlClient
Imports System.Net
Imports System.Net.Mail
Imports System.IO

Partial Class any_kind_refer_pingIP
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Call Ping("C1012849807") 'ping 刷卡機

End Sub

Private Sub Ping(ByVal ip As String)

Dim o As Object, p As Object, s As Object
Dim i As Integer
Dim PtcAddr As String
Dim Pingtimes As Integer = 0

o = GetObject("winmgmts:{impersonationLevel=impersonate}")
For i = 0 To 3
p = o.ExecQuery("Select * From Win32_PingStatus Where Address = '" & ip & "'")
For Each s In p
If (s.StatusCode) <> Nothing Or s.StatusCode <> 0 Then
'Ping = "Computer " & ip & " is not reachable"
Pingtimes -= 1

Else '
PtcAddr = s.ProtocolAddress
'Ping = Ping() & vbCrLf & _
' "Bytes=" & s.BufferSize & " " & _
' "Time=" & s.ResponseTime & "ms " & _
' "TTL=" & s.ResponseTimeToLive
Pingtimes += 1

End If
Next
Next

If Pingtimes >= 1 Then '代表電腦有開機

Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "javascript", "window.parent.frames(3).location='work.aspx'", True)

ElseIf Pingtimes <= 0 Then '代表電腦無開機
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "javascript", "window.parent.frames(3).location='errorweb.aspx'", True)

End If
'Ping = "Pinging " & ip & _
' IIf(ip = PtcAddr, "", " [" & PtcAddr & "] ") & vbCrLf & Ping

End Sub

End Class

看更多先前的討論...收起先前的討論...
slime iT邦大師 1 級 ‧ 2013-02-19 11:54:46 檢舉
有很難嗎?
第一部份應該少 copy 了一些東西, 程式不太完整.
第二部份 Private Sub Ping 用了兩種方式去試著查某 IP 可否連線, 依連線狀況顯示不同的網頁.

其他的都是呼叫連線測試的方法與判斷.
slime iT邦大師 1 級 ‧ 2013-02-19 12:00:20 檢舉
用 batch 改寫大概像這樣:
1.bat
call ping.bat C1012849807

ping.bat
@echo off
ping %1 > NUL
if %ERRORLEVEL% EQU 0 explorer "work.aspx" else explorer "errorweb.aspx"
louis1w iT邦新手 3 級 ‧ 2013-02-19 14:44:30 檢舉
我都靠這個轉碼
http://www.developerfusion.com/tools/convert/vb-to-csharp/
以下參考結果
using Microsoft.VisualBasic;
//using 部份略過(字數太多)
partial class any_kind_refer_pingIP : System.Web.UI.Page
{
Protected Page_Load(object sender, System.EventArgs e)
{
Ping("C1012849807");
}
private void Ping(string ip)
{
object o = null;
object p = null;
object s = null;
int i = 0;
string PtcAddr = null;
int Pingtimes = 0;
o = Interaction.GetObject("winmgmts:impersonationLevel=impersonate}");
for (i = 0; i <= 3; i++)
{p = o.ExecQuery("Select * From Win32_PingStatus Where Address = '" + ip + "'");
foreach (object s_loopVariable in p)
{s = s_loopVariable;
if ((s.StatusCode) != null | s.StatusCode != 0)
{Pingtimes-= 1; }
else { PtcAddr = s.ProtocolAddress; Pingtimes += 1; }
}}//代表電腦有開機
if (Pingtimes >= 1) { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "javascript", "window.parent.frames(3).location='work.aspx'", true);//字數過多了
louis1w iT邦新手 3 級 ‧ 2013-02-19 14:45:39 檢舉
//接上補完
//代表電腦無開機
} else if (Pingtimes <= 0) {
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "javascript", "window.parent.frames(3).location='errorweb.aspx'", true);
}
}
public any_kind_refer_pingIP()
{
Load += Page_Load;
}
謝謝樓上兩位的回答 我看的懂CODE在幹嘛 但是轉成C#就是一直產生錯誤

for (i = 0; i <= 3; i++)
{
p = o.ExecQuery("Select * From Win32_PingStatus Where Address = '" + ip + "'");
foreach (object s_loopVariable in p)
{
s = s_loopVariable;
if ((s.StatusCode) != null || s.StatusCode != 0)
{
//Ping = "Computer " & ip & " is not reachable"
Pingtimes -= 1;


//
}
else
{
PtcAddr = s.ProtocolAddress;
//Ping = Ping() & vbCrLf & _
// "Bytes=" & s.BufferSize & " " & _
// "Time=" & s.ResponseTime & "ms " & _
// "TTL=" & s.ResponseTimeToLive
Pingtimes += 1;

}
}
}

錯誤的地方集中在這個迴圈裡 主要是EXECQUERY沒有定義 應該是缺了什麼命名空間
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
6
careychen
iT邦研究生 5 級 ‧ 2013-03-09 16:55:01
最佳解答

HI, 有個網站可以給您參考一下,其實 c# 有的部份不用這樣子和 VB 寫的方式直接對轉,參考看看

參考網址

8
louis1w
iT邦新手 3 級 ‧ 2013-02-19 14:53:14

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Management;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.IO;
partial class any_kind_refer_pingIP : System.Web.UI.Page
{Protected void Page_Load(object sender, System.EventArgs e)
{
Ping("C1012849807");//ping 刷卡機
}
private void Ping(string ip)
{
object o = null;
object p = null;
object s = null;
int i = 0;
string PtcAddr = null;
int Pingtimes = 0;
o = Interaction.GetObject("winmgmts:{impersonationLevel=impersonate}");
for (i = 0; i <= 3; i++) {
p = o.ExecQuery("Select * From Win32_PingStatus Where Address = '" + ip + "'");
foreach (object s_loopVariable in p) {
s = s_loopVariable;
if ((s.StatusCode) != null | s.StatusCode != 0) {
//Ping = "Computer " & ip & " is not reachable"
Pingtimes -= 1;
//
} else {
PtcAddr = s.ProtocolAddress;
//Ping = Ping() & vbCrLf & _
// "Bytes=" & s.BufferSize & " " & _
// "Time=" & s.ResponseTime & "ms " & _
// "TTL=" & s.ResponseTimeToLive
Pingtimes += 1;
}
}
}
//代表電腦有開機
if (Pingtimes >= 1) {
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "javascript", "window.parent.frames(3).location='work.aspx'", true);
//代表電腦無開機
} else if (Pingtimes <= 0) {
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "javascript", "window.parent.frames(3).location='errorweb.aspx'", true);
}
//Ping = "Pinging " & ip & _
// IIf(ip = PtcAddr, "", " [" & PtcAddr & "] ") & vbCrLf & Ping
}
public any_kind_refer_pingIP()
{
Load += Page_Load;
}
//放在討論字數有限制 這裡再放一次

12
sam0407
iT邦大師 1 級 ‧ 2013-02-20 08:42:59

其實不需要轉換,直接將VB.NET的檔案新增到C#的專案裡也是可以的!

我要發表回答

立即登入回答