Skip to content

Instantly share code, notes, and snippets.

@bradmartin333
Created June 23, 2021 10:55
Show Gist options
  • Save bradmartin333/df4cf293980164db62b01b0e7bff4229 to your computer and use it in GitHub Desktop.
Save bradmartin333/df4cf293980164db62b01b0e7bff4229 to your computer and use it in GitHub Desktop.
Toggle GigE Network Card
// Components.cs //
public static void Initiliaze()
{
try
{
Components.Cam = new Camera();
OpenCamera();
}
catch (Exception)
{
Functions.CamNetDisable();
Functions.CamNetEnable();
while (Components.Cam == null)
{
try
{
Components.Cam = new Camera();
}
catch (Exception)
{
System.Threading.Thread.Sleep(3000); // Wait for camera to be available
}
}
// Wait for camera to be ready to open
// 3 seconds was not enough
// 10 seconds is overkill
for (int i = 0; i < 100; i++)
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
OpenCamera();
}
}
// Functions.cs //
private static readonly string interfaceName = "GigECameraA"; // netsh functions only work when running as administrator
public static void CamNetEnable()
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" enable")
{
UseShellExecute = false,
CreateNoWindow = true
};
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = psi;
p.Start();
p.WaitForExit(5000);
}
public static void CamNetDisable()
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" disable")
{
UseShellExecute = false,
CreateNoWindow = true
};
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = psi;
p.Start();
p.WaitForExit(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment