== Common C# Libraries == === File and Directory Manipulation === * Create a directory: using System.IO; class DirectoryUtil { public static void Main() { Directory.CreateDirectory("C:\\projdir"); } } * Create subdirectory: Directory.CreateDirectory("C:\\projdir\\subdir1"); * Move a subdirectory: Directory.Move("C:\\projdir\\subdir", "C:\\projdir2"); * Delete directory: Directory.Delete("C:\\projdir"); * Copy or Rename a file: File.Copy( "c:\\file1.jpg", "c:\\temp\\file2.jpg"); * Move a file: File.Move( "c:\\file1.jpg", "c:\\temp\\file1.jpg"); * Delete a file: File.Delete( "c:\\file1.jpg" ); * Check if directory exists: if (Directory.Exists(folderBrowserDialog1.SelectedPath)) { // do something } === Path Manipulation === * See full list of available ''System.IO.Path'' methods: [[http://msdn.microsoft.com/en-us/library/system.io.path_members.aspx|Path Members]] * Get FileName or FilePath (See [[http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx|Path.GetFileName() Method]] or [[http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx|Path.GetFileNameWithoutExtension() Method]]): string fileName = @"C:\mydir\myfile.ext"; string path = @"C:\mydir\"; string result; result = Path.GetFileName(fileName); Console.WriteLine("GetFileName('{0}') returns '{1}'", fileName, result); result = Path.GetFileName(path); Console.WriteLine("GetFileName('{0}') returns '{1}'", path, result); // This code produces output similar to the following: // // GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext' // GetFileName('C:\mydir\') returns '' * Get file list: public void GetFileList(dirpath) { DirectoryInfo dir = new DirectoryInfo(dirpath); FileInfo[] dirfiles; string filemask = "*.jpg"; if (filemask != "") { dirfiles = dir.GetFiles(filemask); } else { dirfiles = dir.GetFiles(); } foreach(FileInfo f in dirfiles) { lstImgLibrary.Items.Add(f.Name); } } * Get application startup path: public string GetAppPath() { return Application.StartupPath; } * Get temporary path: string m_Filename = System.IO.Path.GetTempPath() + "\\" + filename; === Registry === * Read registry key: using Microsoft.Win32; ... RegistryKey hklm = Registry.LocalMachine; hklm=hklm.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"); Object obp = hklm.GetValue("Identifier"); Console.WriteLine("Processor Identifier :{0}",obp); * Delete registry key: RegistryKey OurKey = Registry.Users; // Create OurKey set to HKEY_USERS OurKey = OurKey.OpenSubKey(".DEFAULT", true ); // Set it to HKEY_USERS\.DEFUALT OurKey.CreateSubKey("OurSubKey"); // Create the key HKEY_USERS\.DEAFULT\OurSubKey OurKey.CreateSubKey(@"OurSubKey\Subkey"); // Create a sub key HKEY_USERS\.DEFAULT\OurSubKey\Subkey OurKey.DeleteSubKey(@"OurSubKey\SubKey"); // Delete the subkey name "subkey" OurKey.DeleteSubKeyTree("OurSubKey"); // Delete the whole subkey and any subkeys below it * Other examples: private string GetNoahModulesPath() { // The name of the key must include a valid root. const string userRoot = "HKEY_LOCAL_MACHINE"; const string subkey = @"\Software\HIMSA\NOAH\Version 3.0\Modules"; const string keyName = userRoot + subkey; return (string)Registry.GetValue(keyName, "DefaultPath", @"C:\Program Files\HIMSA\Modules"); } private string GetNoahVersion() { // The name of the key must include a valid root. const string userRoot = "HKEY_LOCAL_MACHINE"; const string subkey = @"\Software\HIMSA\NOAH\Version 3.0"; const string keyName = userRoot + subkey; string NhVersion = "0"; if (Registry.GetValue(keyName, "", "") != null) { NhVersion = "3.0"; } else { NhVersion = "0"; } return NhVersion; } == Graphics == === Screen Capture === Source: William Blum, http://www.wblum.org/smallsamp/copyclient.html private Bitmap MakeImageOfClient() { Bitmap bm; using (Graphics g = CreateGraphics()) { bm = new Bitmap(ClientSize.Width, ClientSize.Height, g); using (Graphics h = Graphics.FromImage(bm)) { Point source = PointToScreen(Point.Empty); h.CopyFromScreen(source, Point.Empty, ClientSize); } } //bm.Save("C:\tmp\sample.bmp"); return bm; }