using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Diagnostics;
namespace ConsoleApplication2
{
class Program
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern bool SetWindowText(IntPtr hwnd, string title);
public static void callBatch(string batchcmd)
{
try
{
// Process.Start(@"C:\\batch1.bat -option xmlfile.xml -text test");
Process.Start(batchcmd);
}
catch (Exception ex)
{
// MessageBox.Show(ex.ToString(), "Batch file failed" , MessageBoxButtons.OK);
Console.WriteLine(ex.Message);
}
}
private static void SetWindowText(string text)
{
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
IntPtr hWnd = currentProcess.MainWindowHandle;//FindWindow(null, "Your console windows caption"); //put your console window caption here
if (hWnd != IntPtr.Zero)
{
//Hide the window
SetWindowText(hWnd, text); // 0 = SW_HIDE
}
}
private static void HideWindow()
{
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
IntPtr hWnd = currentProcess.MainWindowHandle;//FindWindow(null, "Your console windows caption"); //put your console window caption here
if (hWnd != IntPtr.Zero)
{
//Hide the window
ShowWindow(hWnd, 0); // 0 = SW_HIDE
}
}
public static void Main(string[] args)
{
Console.Title = "DetectEthernet";
SetWindowText("DetectEthernet");
//String ethername = ConfigurationSettings.AppSettings["ethernet"];
//string ethername=args[0];
string batchcmd = args[0];
// Network Interface: 乙太網路, Status: Up
NetworkChange.NetworkAddressChanged += (s, e) =>
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (var item in nics)
{
if(args.Contains(item.Name) && item.OperationalStatus==OperationalStatus.Up) // 乙太網路 && Up
{
callBatch(batchcmd);
break;
}
//Console.WriteLine("Network Interface: {0}, Status: {1}", item.Name, item.OperationalStatus.ToString());
}
};
string input = string.Empty;
HideWindow();
while (input != "quit")
{
input = Console.ReadLine();
}
}
}
}
2013年9月17日 星期二
Winodows 偵測網路線是否插上或拔下 .NET sample
訂閱:
意見 (Atom)