首页 > 代码库 > C#学习笔记 --文件和注册表操作
C#学习笔记 --文件和注册表操作
System.MarshalByRefObject --这是.NET类中用于远程操作的基对象类,它允许在应用程序域之间编组数据。
FileSystemInfo --这是表示任何文件系统对象的基类
FileInfo和File --这些类表示文件系统上的文件
DirectoryInfo和Directory --这些类表示文件系统上的文件夹
Path --这个类包含的静态成员可以用于处理路径名
DriveInfo --它的属性和方法提供了指定驱动器的信息
Directory类和File类只包含静态方法,不能被实例化
File.Copy(@"C:\Program Files\My Program\ReadMe.txt".@"D:\Copies\ReadMe.txt");
DirectoryInfo类和FileInfo类实现与Directory类和File类大致相同的公共方法,需要实例化
FileInfo myFile = new FileInfo(@"C:\Program Files\My Program\ReadMe.txt");myFile.CopyTo(@"D:\Copies\ReadMe.txt");
Path类不能实例化
移动和删除文件或文件夹可以使用FileInfo和DirectoryInfo类的MoveTo()和DeleteTo()方法来完成
File和Directory类这两个对应方法是Move()和Delete()。
FileInfo和File类分别实现CopyTo()和Copy()方法
File.ReadAllText() 读取文件
File.ReadAllText(FilePath);
File.WriteAllText()
File.WriteAllText(FilePath,Contents);//指定新文件的编码格式File.WriteAllText(FilePath,Contents,Encoding);
文件的读写,最常用的类如下:
FileStream(文件流)--这个类主要用于在二进制文件中读写二进制数据--也可以使用它读写任何文件
StreamReader(流读取器)和StreamWriter(流写入器)--这两个类专门用于读写文本文件
构造FileStream实例,需要4条信息:
要访问的文件
表示如何打开文件的模式--FileMode
表示访问文件的方式--是只读、只写还是读写--FileAccess
共享访问--表示是否独占访问文件--FileShare
FileStream fs3 = new FileStream(@"C:\C# Projects\Project3.doc",FileMode.Create,FileAccess.Write,FileShare.None);FileInfo myFile4 = new FileInfo(@"C:\C# Projects\Project4.doc");FileStream fs4 = myFile4.OpenRead();FileInfo myFile5 = new FileInfo(@"C:\C# Projects\Project5doc");FileStream fs5 = myFile5.OpenWrite();FileInfo myFile6 = new FileInfo(@"C"\C# Projects\Project6doc");FileStream fs6 = myFile6.Open(FileMode.Append,FileAccess.Write,FileShare.None);FileInfo myFile7 = new FileInfo(@"C:\C# Projects\Project7.doc");FileStream fs7 = myFile7.Create();//关闭流fs.Close();int NextByte = fs.ReadByte();int nBytesRead = fs.Read(ByteArray,0,nBytes);byte NextByte = 100;fs.WriteByte(NextByte);fs.Write(ByteArray,0,nBytes);
StreamReader只能执行读取操作
StreamReader sr = new StreamReader(@"C:\My Documents\ReadMe.txt",Encoding.UTF8);FileStream fs = new FileStream(@"C:\My Documents\ReadMe.txt",FileMode.Open,FileAccess.Read,FileShare.None);StreamReader sr = new StreamReader(fs);FileInfo myFile = new FileInfo(@"C:\My Documents\ReadMe.txt");StreamReader sr = myFile.OpenText();string nextLine = sr.ReadLine();string restOfStream = sr.ReadToEnd();int nextChar = sr.Read();int nChars = 100;char[] charArray = new char[nChars];int nCharsRead = sr.Read(charArray,0,nChars);
StreamWriter只能用于写入文件
StreamWriter sw = new StreamWriter(@"C:\My Documents\ReadMe.txt",true,Encoding.ASCII);FileStream fs = new FileStream(@"C:\My Documents\ReadMe.txt",FileMode.CreateNew,FileAccess.Write,FileShare.Read);StreamWriter sw = new StreamWriter(fs);FileInfo myFile = new FileInfo(@"C:\My Documents\NewFile.txt");StreamWriter sw = myFile.CreateText();sw.Close()string nextLine = "Groovy Line";sw.Write(nextLine);char[] charArray = new char[100];sw.Write(charArray);
文件的安全性
从文件中读取ACL
从目录中读取ACL
添加和删除文件中的ACL项
读写注册表
注册表项可以格式化为3个数据类型:
REZ_SZ
REG_DWORD
RED_BINARY
访问注册表,Microsoft.Win32名称空间中的两个类Registry和RegistryKey
Registry类通过静态属性来提供这些实例,这些属性有7个,分别是ClassesRoot、CurrentConfig、CurrentUser、DynData、LocalMachine、PerformanceDate和Users
如果要实例化一个RegistryKey对象,唯一的方式就是从Registry类的对应静态属性开始,向下浏览
以下方式只读访问
RegistryKey hklm = Registry.LocalMachine;RegistryKey hkSoftware = hklm.OpenSubKey("Software");RegistryKey hkMicrosoft = hkSoftware.OpenSubKey("Microsoft");
以下读写访问
RegistryKey hklm = Registry.LocalMachine;RegistryKey hkSoftware = hklm.OpenSubKey("Software");RegistryKey hkMicrosoft = hkSoftware.OpenSubKey("Microsoft",true);
创建一个键,使用CreateSubKey()方法
RegistryKey hklm = Registry.LocalMachine;RegistryKey hkSoftware = hklm.OpenSubKey("Software");RegistryKey hkMicrosoft = hkSoftware.CreateSubKey("MyOwnSoftware");
使用SetValue()和GetValue()方法设置或获取该键中的数据
SetValue()方法根据所提供的类的类型,确定把值设置为REG_SZ、REG_DWORD,还是REG_BINARY
RegistryKey hkMine = HkSoftware.CreateSubKey("MyOwnSoftware");hkMine.SetValue("MyStringValue","Hello World");hkMine.SetValue("MyIntValue",20);
RegistryKey.GetValue()方法返回一个对象引用
string stringValue = http://www.mamicode.com/(string)hkMine.GetValue("MyStringValue");int intValue = http://www.mamicode.com/(int)hkMine.GetValue("MyIntValue");//关闭该键hkMine.Close();
读写独立存储器
可以轻松地存储应用程序状态或用户设置
SaveSettings()方法
using System.IO;using System.IO.IsolatedStorage;using System.Textvoid SaveSettings(){ IsolatedStorageFile storFile = IsolatedStorageFile.GetUserStoreForDomain(); IsolatedStorageFileStream storStream = new IsolatedStorageFileStream("SelfPlacingWindow.xml",FileMode.Create,FileAccess.Write); System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(storeStream,Encoding.UTF8);
writer.WriteStartDocument(); writer.WriteStartElement("Settings"); writer.WriteStartElement("BackColor"); writer.WriteValue(BackColor.ToKnownColor().ToString()); writer.WriteEndElement(); writer.WriteStartElement("Red"); writer.WriteValue(BackColor.R); writer.WriteEndElement(); writer.WriteStartElement("Green"); writer.WriteValue(BackColor.G); writer.WriteEndElement(); writer.WriteStartElement("Blue"); writer.WriteValue(BackColor.B); writer.WriteEndElement(); writer.WriteStartElement("Width"); writer.WriteValue(Width); writer.WriteEndElement(); writer.WriteStartElement("Height"); writer.WriteValue(Height); writer.WriteEndElement(); writer.WriteStartElement("X"); writer.WriteValue(DesktopLocation.X); writer.WriteEndElement(); writer.WriteStartElement("Y"); writer.WriteValue(DesktopLocation.Y); writer.WriteEndElement(); writer.WriteStartElement("WindowState"); writer.WriteValue(WindowState.ToString()); writer.WriteEndElement(); writer.WriteEndElement(); writer.Flush(); writer.Close(); storStream.Close(); storFile.Close(); }
从存储器中读取数据通过ReadSettings()方法实现
bool ReadSettings(){ IsolatedStorageFile storFile = IsolatedStorageFile.GetUserStoreForDomain(); string[] userFiles = storFile.GetFileNames("SelfPlacingWindow.xml"); foreach(string userFile in userFiles) { if(userFile == "SelfPlacingWindow.xml") { listBoxMessages.Items.Add("Successfully opened file" + userFile.ToString()); StreamReader storStream = new StreamReader(new IsolatedStorageFileStream("SelfPlacingWindow.xml",FileMode.Open,StorFile)); System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(storStream); int redComponent = 0; int greenComponent = 0; int blueComponent = 0; int X = 0; int Y = 0; while(reader.Read()) { switch(reader.Name) { case "Red" redComponent = int.Parse(reader.ReadString()); break; case "Green" greenComponent = int.Parse(reader.ReadString()); break; case "Blue" blueComponent = int.Parse(reader.ReadString()); break; case "X" X = int.Parse(reader.ReadString()); break; case "Y" Y = int.Parse(reader.ReadString()); break; case "Red" redComponent = int.Parse(reader.ReadString()); break; case "Width" this.Width = int.Parse(reader.ReadString()); break; case "Height" this.Height= int.Parse(reader.ReadString()); break; case "WindowState" this.WindowState= (FormWindowState)FormWindowState.Parse(WindowState.GetType(),reader.ReadString())); break; default: break; } } this.BackColor = Color.FromArgb(redComponent,greenComponent,blueComponent); this.DesktopLocation = new Point(X,Y); listBoxMessages.Items.Add("Background color:"+BackColor.Name); listBoxMessages.Items.Add("Desktop location:" + DesktopLocation.ToString()); listBoxMessages.Items.Add("Size:" + new Size(Width,Height).ToString()); storStream.Close(); storFile.Close(); } } return true;}
C#学习笔记 --文件和注册表操作