I am a starter in c# and I have a small knowledge. I have made a windows application on c# that shutdwon windows servers in my network remotely. I have a v-center server that hosts two Hosts with virtual machines. I could connect to the virtual machines and shut them down but my issue is I tried to write a code to shutdwon the host itself using VIX API in c#, but I couldn't. All I get is to disconnect them. Am I missing any other class or sdks???
try
{
VMWareVirtualHost host = new VMWareVirtualHost();
host.ConnectToVMWareVIServer("172.16.1.72", "root","123456");
//host.Disconnect();
IVMWareVirtualMachine machine = new VMWareVirtualMachine();
machine = host.Open("[datastore1] Kerio contarol/Kerio contarol.vmx");
machine.ShutdownGuest();
if (machine.IsRunning == true)
{
MessageBox.Show("Machine is running");
}
else
{
MessageBox.Show("Machine is not rinning");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
I think that you can try the PowerOff function.
try
{
VMWareVirtualHost host = new VMWareVirtualHost();
host.ConnectToVMWareVIServer("172.16.1.72", "root","123456");
//host.Disconnect();
IVMWareVirtualMachine machine = new VMWareVirtualMachine();
machine = host.Open("[datastore1] Kerio contarol/Kerio contarol.vmx");
machine.PowerOff();
if (machine.IsRunning == true)
{
MessageBox.Show("Machine is running");
}
else
{
MessageBox.Show("Machine is not rinning");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Related
Error Java.Net.SocketException: Binding socket to network 626 failed: ENONET (Machine is not on the network)
Xamarin app: this method is written for android.
I am getting this error, it is not reported every time, it is difficult for me to capture it in test environments.
Why is this error, what is the explanation?
what can be done to have better control in this method?
There is a similar question asked some time ago, but it does not the same operation that I do here, and it is not solved.
public static void BindSocketToWifi(Socket socket)
{
try
{
ConnectivityManager connectivityManager = (ConnectivityManager)Android.App.Application.Context.GetSystemService(Context.ConnectivityService);
NetworkInfo wifiInfo = null;
var networks = connectivityManager.GetAllNetworks();
foreach (var net in networks)
{
wifiInfo = connectivityManager.GetNetworkInfo(net);
if (wifiInfo.Type == ConnectivityType.Wifi)
{
try
{
net.BindSocket((Socket)socket);
}
catch (Exception ex)
{
Crashes.TrackError(ex);
throw ex;
}
break;
}
}
}
catch (Exception e)
{
Crashes.TrackError(e);
throw e;
}
}
The application, not ASP.NET, uses the below code to authenticate with windows.
Run the application as a normal process and it fails to authenticate with either local machine or domain. "The handle is invalid" is the exception for local machine.
Run it as administrator and it works fine for both domain and machine. Any idea?
It did use to work fine for both, so it is not impossible that there was a policy change in the security settings.
private bool VerifyWindowsPassword()
{
bool ret = false;
string username = this.usernameTextBox.Text;
string unsecure = ConvertToUNSecureString(this.Password);
try
{
var context = new PrincipalContext(ContextType.Domain);
try
{
ret = context.ValidateCredentials(username, unsecure);
if (ret)
{
return true;
}
}
catch
{
}
context = new PrincipalContext(ContextType.Machine);
ret = context.ValidateCredentials(username, unsecure);
if (ret)
{
return true;
}
if (!ret)
{
MessageBox.Show(
"Windows User/Password could not be authenticated.",
"Settings",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(
string.Format("Windows User/Password could not be authenticated. {0}", ex.Message),
"Settings",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
return ret;
}
I want to scan a network and enumerate hostname of all windows machines. There is an interface method that takes an ip range as input and returns hostnames. I have to implement it. So, here is my code:
public ICollection<string> EnumerateWindowsComputers(ICollection<string> ipList)
{
ICollection<string> hostNames = new List<string>();
foreach (var ip in ipList)
{
var hostName = GetHostName(ip);
if (string.IsNullOrEmpty(hostName) == false)
{
hostNames.Add(hostName)
}
}
return hostNames;
}
private static string GetHostName(string ipAddress)
{
try
{
IPHostEntry entry = Dns.GetHostEntry(ipAddress);
if (entry != null)
{
return entry.HostName;
}
}
catch (SocketException ex)
{
System.Console.WriteLine(ex.Message + " - " + ipAddress);
}
return null;
}
This method enumerates all windows machines successfully, but there are network printers in it. I can easily ignore my printers' hostname, but it will not be a good solution. I have to make sure that only the devices with the Windows operating system returned.
Any idea how to do it without a third party library? If there is a better way, we don't have to use GetHostName method.
P.S. Linux, MacOS, Android and IOS devices are not found as expected.
Service detection would not be true as there may be linux or other box emulating Windows FileSharing
Use systeminfo /s IPADDRESS shell command from Windows Machine to reliably fetch remote Windows OS details. You code will be like following:
string IPADDRESS = "192.168.1.1";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.startInfo.FileName = "cmd.exe";
p.startInfo.Arguments = "/C systeminfo /s IPADDRESS";
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
if(output.Contains("Microsoft Windows")) { Console.WriteLine("Windows OS"); }
One way you can attempt to detect the OS in a remote machine is through the use of ping. Ping each IP address and get the TTL. That should give you an idea of the OS you're dealing with. A table matching TTL to OS can be found here: http://www.kellyodonnell.com/content/determining-os-type-ping
According to #Jeroen van Langen's comment, I changed my GetHostName method with GetWindowsHostName.
private string GetWindowsHostName(string ipAddress)
{
try
{
IPHostEntry entry = Dns.GetHostEntry(ipAddress);
if (entry != null)
{
try
{
using (TcpClient tcpClient = new TcpClient())
{
// 445 is default TCP SMB port
tcpClient.Connect(ipAddress, 445);
}
using (TcpClient tcpClient = new TcpClient())
{
// 139 is default TCP NetBIOS port.
tcpClient.Connect(ipAddress, 139);
}
return entry.HostName;
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
}
catch (SocketException ex)
{
System.Console.WriteLine(ex.Message + " - " + ipAddress);
}
return null;
}
There can be false positive, but this is unlikely and acceptable for me.
I have a C# application that is self hosting a WCF service. I want to add a button click event in the application that lets the user know if the service is running/being hosted. Is there a way to detect if the service is running/hosted?
In case someone wants to see it, here is the code I am using to start hosting the service:
private static void RunService()
{
System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(AccountingOperationsService.AccountingOperationsService));
System.ServiceModel.Description.ServiceDebugBehavior debug = host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceDebugBehavior>();
// if not found - add behavior with setting turned on
if (debug == null)
{
host.Description.Behaviors.Add(
new System.ServiceModel.Description.ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
}
else
{
// make sure setting is turned ON
if (!debug.IncludeExceptionDetailInFaults)
{
debug.IncludeExceptionDetailInFaults = true;
}
}
try
{
host.Open();
}
catch (Exception ex)
{
string errorMessage = ex.Message + Environment.NewLine;
errorMessage += ex.StackTrace + Environment.NewLine;
DevExpress.XtraEditors.XtraMessageBox.Show(errorMessage, "Error Starting Service", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Perhaps, you need create method Ping in wcf service.
public bool Ping()
{
return true;
}
and in application call Ping
bool itsWork;
try
{
itsWork = service.Ping();
}
catch(Exception ex){}
I am writing a pc & phone communication app. The pc code is written in C#. When I connect my phone (which is paired with pc) to my pc, the phone connects however the logs say, the socket is not connected i-e; socket.isConnected() returns false on my phone. I have tried to run this app on multiple phones and the problem persists.
I think the issue is with my C# code as I don't know C# well.
Following is the c# code:
BluetoothListener blueListener = new BluetoothListener(mUUID);
blueListener.Start();
BluetoothClient conn = blueListener.AcceptBluetoothClient();
if(conn.Connected)
updateUI("Mobile connected");
Stream mStream = conn.GetStream();
while (true)
{
try
{
byte[] received = new byte[1024];
mStream.Read(received, 0, received.Length);
//.....
}
catch() {}
}
I am using 32feet library in C#.
Following is my Android code:
BluetoothSocket socket = selectedDevice.createRfcommSocketToServiceRecord(deviceUUID);
try
{
socket = openSocketForSendingData();
try
{
BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter();
bAdapter.cancelDiscovery();
socket.connect();
Log.e("NP socket", "No Exception");
}
catch(Exception e)
{
Log.e("NP socket Exception", e.toString());
}
if(socket == null)
Log.e("Socket NULL", "socket is null");
if(!socket.isConnected())
{
Log.e("NP socket", "socket not cconnected");
try
{
socket.close();
}
catch(Exception e)
{
Log.e("Socket CLOSE",e.toString());
}
}
else
Log.e("NP socket", "socket cconnected");
if(socket.getOutputStream() == null)
Log.e("Soccket Outupt", "output stream null");
dos = new DataOutputStream(socket.getOutputStream());
dos.writeChars("Hello World I am android");
}
catch(Exception e)
{
Log.e("NP writing socket", e.toString());
}
The UUID in both codes is same.
Can you guide me what mistake am I making?
PS: Before asking this question, I have already spent all day searching the solution on internet, but it was no good.
Regards