Torna al Thread

using System; using System.Runtime.InteropServices; namespace shutdown { class Program { [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InitiateSystemShutdown( string lpMachineName, string lpMessage, uint dwTimeout, bool bForceAppsClosed, bool bRebootAfterShutdown); static void Main(string[] args) { using (new NetConnect(@"\\192.168.x.x", "nomeutente", "password")) { bool result = false; Console.WriteLine("Premere un tasto per spegnere il pc..."); Console.ReadKey(); result = InitiateSystemShutdown(@"\\192.168.x.x", "Shutdown...", 30, false, false); Console.WriteLine("Risultato: " + result); Console.ReadKey(); } } } class NetConnect : IDisposable { private struct NETRESOURCE { public int dwScope; public int dwType; public int dwDisplayType; public int dwUsage; [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpLocalName; [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpRemoteName; [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpComment; [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpProvider; } private const int RESOURCETYPE_ANY = 0x00000000; private const int CONNECT_TEMPORARY = 0x00000004; [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2W", CharSet=System.Runtime.InteropServices.CharSet.Unicode)] private static extern int WNetAddConnection2(ref NETRESOURCE lpNetResource, string lpPassword, string lpUsername, Int32 dwFlags); public void Dispose() { // Qui si potrebbe annullare la connessione... } public NetConnect(string RemoteResource, string Username, string Password) { int result; NETRESOURCE nr = new NETRESOURCE(); nr.dwScope = 0; nr.dwType = RESOURCETYPE_ANY; nr.dwDisplayType = 0; nr.dwUsage = 0; nr.lpLocalName = null; nr.lpRemoteName = RemoteResource + "\0"; nr.lpComment = null; nr.lpProvider = null; result = WNetAddConnection2(ref nr, Password, Username, CONNECT_TEMPORARY); // Si potrebbe interpretare result in cerca di errori... } } }
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5