Showing posts with label Sysprogrtamming. Show all posts
Showing posts with label Sysprogrtamming. Show all posts

Aug 11, 2011

How to retrieve Application Data Folder using C# (.NET)


ApplicationData is the directory that serves as a common repository for application-specific data for the current roaming user. It can be retrieved using



current user:
Console.WriteLine("ApplicationData := " +Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));



This example produces the following results:

GetFolderPath: C:\WINNT\System32


Accessing Application data folder path for all windows users



 MessageBox.Show("ApplicationData := " + 
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));







Jul 6, 2011

example for Process. WaitForInputIdle and WaitForExit

Causes the Process component to wait indefinitely for the associated process to enter an idle state. This overload applies only to processes with a user interface and, therefore, a message loop.


using System.Runtime.InteropServices;


 [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImportAttribute("User32.dll")]
        private static extern IntPtr FindWindow(String ClassName, String WindowName);






 public void GetMSInfo()
       {


            try
            {


                string strArguments = " /report" + space.ToString() + path + "\\MS32INFO.txt";
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
                psi.WindowStyle = ProcessWindowStyle.Hidden;
                psi.UseShellExecute = false;
                psi.RedirectStandardOutput = true;
                psi.CreateNoWindow = true;
                psi.FileName = "msinfo32.exe";
                psi.Arguments = strArguments;
                
                System.Diagnostics.Process si = new System.Diagnostics.Process();
                si.StartInfo = psi;
                IntPtr hWnd;
                si.Start();
               
               si.WaitForInputIdle();
                hWnd = FindWindow(null, "System Information");
                ShowWindow(hWnd, 0);
                
                si.WaitForExit();
                
              //  string output = si.StandardOutput.ReadToEnd();
                si.Close();
            }


            catch (Exception ex)
            {
                throw ex;
            }


        }

Jun 22, 2011

Get A List Of Installed Applications Using LINQ And C#


To get a list of installed applications we need to look into registry. Microsoft.Win32 namespace contains objects which can be used to work with Windows Registry. In this post I will show you some code where I use the power of LINQ to retrieve and display a list of all applications installed on a machine.
The basic idea is that we iterate through a collection of RegistryKey objects within LocalMachine\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall. We then open the sub keys and get the DisplayName.
Here is the code:
static void DisplayInstalledApplications()
{
  string registryKey = 
    @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
 
  using (Microsoft.Win32.RegistryKey key =
    Registry.LocalMachine.OpenSubKey(registryKey))
  {
    var query = from a in
              key.GetSubKeyNames()
              let r = key.OpenSubKey(a)
              select new
              {
                Application = r.GetValue("DisplayName")
              };
 
    foreach (var item in query)
    {
      if (item.Application != null)
        Console.WriteLine(item.Application);
    }
  }
}
 
I can also make this a bit more LINQed by removing the foreach loop. It just adds a bit more C# 3.0 flavour to the code and does the retrieval and writing to console in one line.
 
static void DisplayInstalledApplications2()
{
  string registryKey =
    @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
 
  using (Microsoft.Win32.RegistryKey key = 
    Registry.LocalMachine.OpenSubKey(registryKey))
  {
    (from a in key.GetSubKeyNames()
    let r = key.OpenSubKey(a)
    select new
    {
      Application = r.GetValue("DisplayName")
    }).ToList()
      .FindAll(c => c.Application != null)
      .ForEach(c => Console.WriteLine(c.Application));
  }
}