38、获取系统中可用的驱动器列表和系统磁盘图标
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;
public string szDisplayName;
public string szTypeName;
}
[DllImport("shell32")]
private static extern int SHGetFileInfo(string pszPath,int dwFileAttributes,ref SHFILEINFO psfi,int cbFileInfo,int uFlags);
const int SHGFI_ICON = 0x0100;
const int SHGFI_LARGEICON = 0x0000;
static string[] drives;
ImageList img=new ImageList();
private void Form1_Load(object sender, System.EventArgs e)
{
this.listView1.LargeImageList=img;
this.listView1.SmallImageList=img;
this.listView1.StateImageList=img;
drives=Environment.GetLogicalDrives();
for(int i=0;i<drives.Length;i++)
{
string str_temp=drives[i];
this.listView1.Items.Add(str_temp);
this.listView1.Items[i].ImageIndex=i;
}
for(int i=0;i<drives.Length;i++)
{
SHFILEINFO FileInfo=new SHFILEINFO();
SHGetFileInfo(drives[i],0,ref FileInfo,Marshal.SizeOf(FileInfo),SHGFI_ICON | SHGFI_LARGEICON);
Icon myIcon;
myIcon=Icon.FromHandle(FileInfo.hIcon);
img.Images.Add(myIcon);
}
39、设置应用程序的快捷键
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd,int wMsg,int wParam,int lParam);
private const int WM_SETHOTKEY = 0x0032;
private const int HOTKEYF_ALT = 0x0004;
private void Form1_Load(object sender, System.EventArgs e)
{
int wHotkey;
wHotkey=(HOTKEYF_ALT)*0x0100+'A';
SendMessage(this.Handle, WM_SETHOTKEY, wHotkey, 0);
}
40、列举系统中的环境变量(用户的环境变量希望知道的留个言)
private void Form1_Load(object sender, System.EventArgs e)
{
this.listView1.View=View.Details;
this.listView1.GridLines=true;
this.listView1.Columns.Add("环境变量",150,HorizontalAlignment.Left);
this.listView1.Columns.Add("变量值",150,HorizontalAlignment.Left);
ListViewItem myItem;
foreach (DictionaryEntry EnValue in Environment.GetEnvironmentVariables())
{
myItem= new ListViewItem(EnValue.Key.ToString(),0);
myItem.SubItems.Add(EnValue.Value.ToString());
this.listView1.Items.Add(myItem);
}
}
41、得到系统的特殊路径
private void Form1_Load(object sender, System.EventArgs e)
{
//System
this.listView1.View=View.Details;
this.listView1.GridLines=true;
this.listView1.Columns.Add("环境变量",150,HorizontalAlignment.Left);
this.listView1.Columns.Add("变量值",150,HorizontalAlignment.Left);
ListViewItem myItem;
myItem= new ListViewItem(Environment.SpecialFolder.ApplicationData.ToString(),0);
myItem.SubItems.Add(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
this.listView1.Items.Add(myItem);
myItem= new ListViewItem(Environment.SpecialFolder.Favorites.ToString(),0);
myItem.SubItems.Add(Environment.GetFolderPath(Environment.SpecialFolder.Favorites));
this.listView1.Items.Add(myItem);
myItem= new ListViewItem(Environment.SpecialFolder.Cookies.ToString(),0);
myItem.SubItems.Add(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
this.listView1.Items.Add(myItem);
myItem= new ListViewItem(Environment.SpecialFolder.System.ToString(),0);
myItem.SubItems.Add(Environment.GetFolderPath(Environment.SpecialFolder.System));
this.listView1.Items.Add(myItem);
}
42、关闭指定的程序
Process[] myProcesses;
myProcesses = Process.GetProcessesByName("Notepad");
foreach (Process instance in myProcesses)
{
instance.WaitForExit(3000);
instance.CloseMainWindow();
}
本系列文章是作者学习《Visual C#.NET 应用编程150例》(源码)心得笔记,欢迎转载,请注明原文地址,如有疑问,可以通过 [email protected] 联系作者本人。