NinethSense oWnZ mE!: Praveen’s drawing book

Choose a Topic:

Mon
9
Jul '07

Get Environment Variables

This code will show list of Environment Variables in Windows:

 
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
{
     listBox1.Items.Add(de.Key + ” = “ + de.Value);
}
Do not forget to include this:
 
using System.Collections; // for DictionaryEntry
'

Send keystrokes to an application

You can send key stokes virtually to other applications with this:


This code will send F11 key press to Mozilla Firefox, which will make it ‘fullscreen’.
 
    int iHandle = FindWindow(null, “Mozilla Firefox”);
    SetForegroundWindow(iHandle);
  SendKeys.Send(“{F11}”);


FindWindow and SetForegroundWindow are APIs, which you can access with

 
 [DllImport(“user32.dll”)]
        public static extern int FindWindow(
            string lpClassName, // class name 
            string lpWindowName // window name 
        );
        [DllImport(“user32.dll”)]
        public static extern int SetForegroundWindow(
            int hWnd // handle to window
            );



Full cs code:
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
 
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport(“user32.dll”)]
        public static extern int FindWindow(
            string lpClassName, // class name 
            string lpWindowName // window name 
        );
        [DllImport(“user32.dll”)]
        public static extern int SetForegroundWindow(
            int hWnd // handle to window
            );
        public Form1()
        {
            InitializeComponent();
        }
 
       
 
        private void button1_Click(object sender, EventArgs e)
        {
            int iHandle = FindWindow(null, “Mozilla Firefox”);SetForegroundWindow(iHandle);SendKeys.Send(“A”);
 
        }
    }
}
The CodeProject Microsoft Developer Network Official ASP.NET Forums Microsoft .NET Framework Community Microsoft Most Valuable Professional Kidoos forums Microsoft Visual Studio Developer Home Professional Information Technology Solutions Microsoft Research Home Kerala Microsoft Users Group Community Website