In my factory we are working on Tools, sometimes we are doing alignment manually and sometimes we are connecting to the computer of the Tool using Remote Desktop Connection or VNC.
For more security i created an application that can block Remote Desktop Service when working on alignment for protecting people from injury.
The application is working on Windows XP and Windows 7 but i have an issue with Windows 10.
i use these services:
"SessionEnv"
"TermService"
So when i enable the connection I'm starting these services using this code:
List<string> service_name = new List<string>()
{
"SessionEnv",
"TermService"
};
private void Start_Click(object sender, RoutedEventArgs e)
{
try
{
if (!windows.Contains("Windows XP"))
{
foreach (var service in service_name)
{
ServiceController sc = new ServiceController(service);
status = Get_Status(service);
if (status == "Running")
{
continue;
}
else if (status == "Stopped")
{
sc.Start();
}
}
count_running = 2;
count_stopped = 0;
}
if (vnc_installed == true)
{
Start_VNC();
}
ni.ShowBalloonTip(500, "Remote Desktop Enable/Disable Application", "Remote Desktop Connections are enabled", ToolTipIcon.Info);
startup_status = "Open";
File.WriteAllText("c:\\temp\\Startup.txt", startup_status);
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
and for disabling the connection:
private void Stop_Click(object sender, RoutedEventArgs e)
{
try
{
if (!windows.Contains("Windows XP"))
{
foreach (var service in service_name)
{
ServiceController sc = new ServiceController(service);
status = Get_Status(service);
if (status == "Running")
{
sc.Stop();
}
else if (status == "Stopped" )
{
continue;
}
}
count_running = 0;
count_stopped = 2;
}
if (vnc_installed == true)
{
Close_VNC();
}
ni.ShowBalloonTip(500, "Remote Desktop Enable/Disable Application", "Remote Desktop Connections are disabled", ToolTipIcon.Info);
startup_status = "Close";
File.WriteAllText("c:\\temp\\Startup.txt", startup_status);
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
Everything is working fine in Windows XP and 7 like i said but in Windows 10 when i'm trying to stop the service "SessionEnv", instead of changing his status to "Stopped" it stucks on status "Stopping". The only solution to get it running again is to kill the process named: "Service Host: Remote Desktop Services"
and of course i'm getting this error: "Cannot open SessionEnv service on computer"
Thank you for your help
edit: After more testing i found that if i'm opening the Application through Visual Studio everything work, if i'm opening the Application from the bin/Debug folder i'm getting the error.