Archive for Gennaio 19th, 2008

WindowControler - Shutdown,Restart, ….

CodeKeep C# Feed Gennaio 19th, 2008

Description: Ði?u khi?n h? th?ng

Link: http://www.codekeep.net/snippets/207000bf-4e15-4700-a72c-8a4f276b9293.aspx

private void DoEvent()
        {
            Process[] arrProc = Process.GetProcesses();
            foreach ( Process childProc in arrProc)
            {
                System.IntPtr hWnd = childProc.MainWindowHandle;
                if (IsIconic(hWnd))
                {
                    ShowWindowAsync(hWnd, SW_RESTORE);
                    SetForegroundWindow(hWnd);
                    if (!(childProc.MainWindowTitle.Equals(this.Text)))
                    {
                        childProc.CloseMainWindow();
                        childProc.Kill();
                        childProc.WaitForExit();
                    }
                }
            }
            switch (cbEvent.SelectedText)
            {
                case "Shutdown":
                    WindowsController.ExitWindows(RestartOptions.ShutDown, false);
                    break;
                case "Restart":
                    WindowsController.ExitWindows(RestartOptions.Reboot, false);
                    break;
                case "Stand By":
                    WindowsController.ExitWindows(RestartOptions.Suspend, false);
                    break;
                case "Hibernate":
                    WindowsController.ExitWindows(RestartOptions.Hibernate, false);
                    break;
                case "Log Off":
                    WindowsController.ExitWindows(RestartOptions.LogOff, false);
                    break;
            }
        }

WindowControler - Shutdown,Restart, ….

CodeKeep C# Feed Gennaio 19th, 2008

Description: Ði?u khi?n h? th?ng

Link: http://www.codekeep.net/snippets/207000bf-4e15-4700-a72c-8a4f276b9293.aspx

private void DoEvent()
        {
            Process[] arrProc = Process.GetProcesses();
            foreach ( Process childProc in arrProc)
            {
                System.IntPtr hWnd = childProc.MainWindowHandle;
                if (IsIconic(hWnd))
                {
                    ShowWindowAsync(hWnd, SW_RESTORE);
                    SetForegroundWindow(hWnd);
                    if (!(childProc.MainWindowTitle.Equals(this.Text)))
                    {
                        childProc.CloseMainWindow();
                        childProc.Kill();
                        childProc.WaitForExit();
                    }
                }
            }
            switch (cbEvent.SelectedText)
            {
                case "Shutdown":
                    WindowsController.ExitWindows(RestartOptions.ShutDown, false);
                    break;
                case "Restart":
                    WindowsController.ExitWindows(RestartOptions.Reboot, false);
                    break;
                case "Stand By":
                    WindowsController.ExitWindows(RestartOptions.Suspend, false);
                    break;
                case "Hibernate":
                    WindowsController.ExitWindows(RestartOptions.Hibernate, false);
                    break;
                case "Log Off":
                    WindowsController.ExitWindows(RestartOptions.LogOff, false);
                    break;
            }
        }

Get Enum friendly description from Description Attribute

CodeKeep C# Feed Gennaio 19th, 2008

Description: Retrieve 'friendly' enum description saved in the DescriptionAttribute

Link: http://www.codekeep.net/snippets/eb024ee9-dc5e-4162-a6d9-70a2a3b372a9.aspx

public class EnumUtilities
{
    public static string GetDescription(object enumValue, string defaultDesc)
    {
        FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());
        if (null != fi)
        {
            object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), true);
            if (attrs != null && attrs.Length > 0)
            {
                return ((DescriptionAttribute)attrs[0]).Description;
            }
        }
        return defaultDesc;
    }
}

String to Stream

CodeKeep C# Feed Gennaio 19th, 2008

Description: Convert string to Stream

Link: http://www.codekeep.net/snippets/54098b93-3947-46aa-8664-1ace7b8d0a04.aspx

string dataToConvert = "some string to convert to stream";
Stream strm = new MemoryStream(System.Text.UTF8Encoding.Default.GetBytes(dataToConvert));