Friday 29 April 2016

How to get Clipboard data and its size in C#.Net

Following function will get the clipboard data:

        [DllImport("user32.dll")]
        static extern IntPtr GetClipboardData(uint uFormat);
        [DllImport("user32.dll")]
        static extern bool IsClipboardFormatAvailable(uint format);
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool OpenClipboard(IntPtr hWndNewOwner);
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool CloseClipboard();
        [DllImport("kernel32.dll")]
        static extern IntPtr GlobalLock(IntPtr hMem);
        [DllImport("kernel32.dll")]
        static extern bool GlobalUnlock(IntPtr hMem);

        const uint CF_UNICODETEXT = 13;
        public static string GetText()
        {
            if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
                return null;
            if (!OpenClipboard(IntPtr.Zero))
                return null;

            string data = null;
            var hGlobal = GetClipboardData(CF_UNICODETEXT);
            if (hGlobal != IntPtr.Zero)
            {
                var lpwcstr = GlobalLock(hGlobal);
                if (lpwcstr != IntPtr.Zero)
                {
                    data = Marshal.PtrToStringUni(lpwcstr);
                    GlobalUnlock(lpwcstr);
                }
            }
            CloseClipboard();

            return data;
        }


In order to get the size of clipboard text, you would need to copy the text in a file and then get the size of file as shown in the below code:

public long getClipboardTextSize()
        {
                string text = GetText();
                System.IO.File.WriteAllText("C:\\ClipboardText.txt", text);
                FileInfo fi = new FileInfo(path);
                if (fi.Exists)
                {
                    return fi.Length;
                }
                else
                {
                    return 0;
                }
            }
        }

Thursday 28 April 2016

Get startup programs of all users using C#.Net

The below code will demonstrate how to list all the startup programs (also listed in msconfig) using C#.Net.

A custom class (cStartupPrograms) is added to store a startup program and it's relevant properties.

 public class cStartupPrograms
    {
        public int StartupProgramId { get; set; }
        public int ComputerId { get; set; }
        public int StartupClassificationId { get; set; }
        public string StartupClassificationName { get; set; }
        public string ProgramName { get; set; }
        public string ProgramPath { get; set; }
        public bool? IsEnabled { get; set; }
        public bool? IsDeleted { get; set; }
        public string StartupType { get; set; }
        public string Publisher { get; set; }
        public string startupUserName { get; set; }
}

Below are the locations from where the startup programs are retrieved:

public static List<string> startupProgramsRegistryNames_Enabled = new List<string>()
        {
            @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", //HKLM, HKCU
            @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run",  //HKLM
            @"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
        };

        public static List<string> startupProgramsRegistryNames_Disabled = new List<string>()
        {
            @"SOFTWARE\Microsoft\Shared Tools\MSConfig\startupfolder",  //HKLM
            @"SOFTWARE\Microsoft\Shared Tools\MSConfig\startupreg"  //HKLM          
        };

        public static List<string> startupProgramsRegistryNames_Enabled_OtherUsers = new List<string>()
        {
            @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", //HKEY_USERS
            @"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
        };

        public static string startupProgramPath = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup";


Below function will retrieve the list of startup program of all users:
private void getAllStartupPrograms()
        {
            List<cStartupPrograms> cStartupProgramList = null;

            RegistryInfo rInfo1 = new RegistryInfo();
            rInfo1.registryKey = Registry.CurrentUser;

            RegistryInfo rInfo2 = new RegistryInfo();
            rInfo2.registryKey = Registry.LocalMachine;

            List<RegistryInfo> rInfoList = new List<RegistryInfo>();
            rInfoList.Add(rInfo1);
            rInfoList.Add(rInfo2);

            try
            {
                cStartupProgramList = new List<cStartupPrograms>();
                //Enabled programs
                //All users
                string[] files = Directory.GetFiles(startupProgramPath, "*");
                foreach (string fileName in files)
                {
                    if (File.Exists(fileName))
                    {
                        string target = GetShortcutTargetFile(fileName);
                        if (!String.IsNullOrEmpty(target))
                        {
                            cStartupPrograms startupProgram = new cStartupPrograms();

                            startupProgram.ComputerId = Convert.ToInt32(frmPreferences.ComputerId);
                            startupProgram.IsDeleted = false;
                            startupProgram.IsEnabled = true;
                            startupProgram.ProgramName = System.IO.Path.GetFileName(fileName);
                            startupProgram.ProgramPath = target;
                            startupProgram.StartupClassificationName = cEnum.eStartupClassification.Windows.ToString();
                            startupProgram.startupUserName = "All Users";
                            startupProgram.StartupType =startupProgramPath;
                           
                            //if (!cStartupProgramList.Exists(p => p.ProgramName == fileName && p.ProgramPath == target))
                            cStartupProgramList.Add(startupProgram);
                        }
                    }
                }

                //All users
                foreach (RegistryInfo rInfo in rInfoList)
                {
                    RegistryKey regKey = rInfo.registryKey;
                    foreach (string registryName in startupProgramsRegistryNames_Enabled)
                    {
                        string runOrRunOnceString = null;
                        int n = registryName.LastIndexOf("\\");
                        runOrRunOnceString = registryName.Substring(n + 1);
                        using (RegistryKey startupKey = regKey.OpenSubKey(registryName))
                        {
                            if (startupKey == null)
                                continue;

                            var valueNames = startupKey.GetValueNames();
                            foreach (var name in valueNames)
                            {
                                cStartupPrograms startupProgram = new .cStartupPrograms();

                                startupProgram.ComputerId = Convert.ToInt32(frmPreferences.ComputerId);
                                startupProgram.IsDeleted = false;
                                startupProgram.IsEnabled = true;
                                startupProgram.ProgramName = name;
                                startupProgram.ProgramPath = Convert.ToString(startupKey.GetValue(name));
                                startupProgram.StartupClassificationName = cEnum.eStartupClassification.Windows.ToString();
                                startupProgram.startupUserName = "All Users";
                                startupProgram.StartupType = startupKey.ToString();

                                //if (!cStartupProgramList.Exists(p => p.ProgramName == name && p.ProgramPath == startupProgram.ProgramPath))
                                cStartupProgramList.Add(startupProgram);
                            }
                        }
                    }
                }

                //Other users
                string[] hkeyUserNames = Registry.Users.GetSubKeyNames();
                foreach (string key in hkeyUserNames)
                {
                    string userName = GetOwnerName(key);
                    if (string.IsNullOrEmpty(userName))
                        continue;

                    RegistryKey startupKey = Registry.Users.OpenSubKey(key);
                    if (startupKey == null)
                        continue;

                    foreach (string registryName in startupProgramsRegistryNames_Enabled_OtherUsers)
                    {
                        string runOrRunOnceString = null;
                        int n = registryName.LastIndexOf("\\");
                        runOrRunOnceString = registryName.Substring(n + 1);

                        using (RegistryKey userKey = startupKey.OpenSubKey(registryName))
                        {
                            if (userKey == null)
                                continue;

                            var valueNames = userKey.GetValueNames();
                            foreach (var name in valueNames)
                            {
                                cStartupPrograms startupProgram = new cStartupPrograms();
                                startupProgram.ComputerId = Convert.ToInt32(frmPreferences.ComputerId);
                                startupProgram.IsDeleted = false;
                                startupProgram.IsEnabled = true;
                                startupProgram.ProgramName = name;
                                startupProgram.ProgramPath = Convert.ToString(userKey.GetValue(name));
                                startupProgram.StartupClassificationName = cEnum.eStartupClassification.Windows.ToString();
                                startupProgram.startupUserName = userName;
                                startupProgram.StartupType = userKey.ToString();
                                //if (!cStartupProgramList.Exists(p => p.ProgramName == name && p.ProgramPath == startupProgram.ProgramPath && p.startupUserName==userName))
                                cStartupProgramList.Add(startupProgram);
                            }
                        }
                    }
                }

                //Disabled programs
                foreach (string registryName in startupProgramsRegistryNames_Disabled)
                {
                    using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) //
                    {
                        using (RegistryKey startupkey = hklm.OpenSubKey(registryName))
                        {

                            if (startupkey == null)
                                continue;

                            foreach (string key in startupkey.GetSubKeyNames())
                            {
                                using (RegistryKey rk = startupkey.OpenSubKey(key))
                                {
                                    cStartupPrograms startupProgram = new cStartupPrograms();

                                    startupProgram.ComputerId = Convert.ToInt32(frmPreferences.ComputerId);
                                    startupProgram.IsDeleted = false;
                                    startupProgram.IsEnabled = false;
                                    startupProgram.ProgramName = Convert.ToString(rk.GetValue("item"));
                                    startupProgram.ProgramPath = Convert.ToString(rk.GetValue("command"));
                                    startupProgram.StartupClassificationName = cEnum.eStartupClassification.Windows.ToString();
                                    var hkeyVal = rk.GetValue("hkey");
                                    if (hkeyVal != null)
                                    {
                                        string keyVal = Convert.ToString(rk.GetValue("key"));
                                        string runOrRunOnceString = null;
                                        int n = keyVal.LastIndexOf("\\");
                                        runOrRunOnceString = keyVal.Substring(n + 1);

                                        if (hkeyVal.ToString() == "HKLM")
                                        {
                                            startupProgram.StartupType = Registry.LocalMachine.ToString() + "\\" + keyVal;
                                           
                                        }
                                        else if (hkeyVal.ToString() == "HKCU")
                                        {
                                            startupProgram.StartupType = Registry.CurrentUser.ToString() + "\\" + keyVal;
                                         
                                        }
                                    }
                                    else
                                    {
                                        startupProgram.StartupType = Convert.ToString(rk.GetValue("location"));
                                    }
                                    startupProgram.startupUserName = "All Users";
                                 
                                    //if (!cStartupProgramList.Exists(p => p.ProgramName == startupProgram.ProgramName && p.ProgramPath == startupProgram.ProgramPath))
                                    cStartupProgramList.Add(startupProgram);

                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
               
                return;
            }
        }

Wednesday 13 April 2016

Convert content of XML file to JSON string in C#.Net

The below function will convert the content of XML file to JSON string:

private string convertXMLToJSON()
        {
            string xmlLogPath = "<<Path to the xml file>>";

            // Load the XML file
            var xDocument = System.Xml.Linq.XDocument.Load(xmlLogPath);

            // Read the XML file content and write it to string
            string xml = xDocument.ToString();

            // Load the XML string
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            // Convert the XML string to JSON string
            string jsonText = JsonConvert.SerializeXmlNode(doc);

            //Release the memory
            doc = null;
            xmlLogPath = null;
            xml = null;
            xDocument = null;
     
            //Return the JSON string
            return jsonText;

        }

IMPORTANT NOTE:

Releasing object memory is very important. Even if your object went out of scope, it is not removed from the garbage collection. It happens because the variable still has some value in it. So Garbage collector think that the variable is in use and it doesn't remove it from the memory.

Setting it to NULL, tells the garbage collector that the object is no longer in use and can be removed from the garbage collection.

Tuesday 12 April 2016

Start and stop a console application using C#

Start a console application:


System.Diagnostics.Process aspProcess = null;
private void start_console_app()
        {
            try
            {
                string pathToExe = @"<<Path to the exe >> ";
                
                if (aspProcess == null)
                {
                    aspProcess = new System.Diagnostics.Process();
                }
                
                //Path and file name of command to run
                aspProcess.StartInfo.FileName = pathToExe;
                aspProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                //Parameters to pass to program
                aspProcess.StartInfo.Arguments = "<<arguments you want to pass>>";

                aspProcess.StartInfo.UseShellExecute = true;
                aspProcess.StartInfo.CreateNoWindow = true;

                //Start the process
                aspProcess.Start();

                //Wait for process to finish
                aspProcess.WaitForExit();
                aspProcess = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

1. For starting a console application, you would need to create an object of class "System.Diagnostics.Process".
2. "FileName" is the path of the exe (console application).
3.  "Arguments" - It will send arguments to your console application.
4.  ProcessWindowStyle.Hidden will hide the console window.
5. Start () method will start the process.

Stop a console application by sending "Control + C" Signal:


public void stop_console_app()
        {
            // Release the current console, as you cannot attach 2 consoles at the same time
            if (aspProcess != null)
            {
                uint pid = (uint)aspProcess.Id;
                FreeConsole();

                // This does not require the console window to be visible.
                if (AttachConsole(pid))
                {
                    // Disable Ctrl-C handling for our program
                    SetConsoleCtrlHandler(null, true);
                    GenerateConsoleCtrlEvent(CtrlTypes.CTRL_C_EVENT, 0);

                    // Must wait here. If we don't and re-enable Ctrl-C
                    // handling below too fast, we might terminate ourselves.
                    Thread.Sleep(2000);

                    FreeConsole();

                    // Re-enable Ctrl-C handling or any subsequently started
                    // programs will inherit the disabled state.
                    SetConsoleCtrlHandler(null, false);
                }
            }
        }

You can add "Console_CancelKeyPress" event in your console application to nicely terminate it. If you want to call this method from another application (from where you've started your console application), add the above method.