.net操作文件,注册表,ACL

文件操作using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace FileBrowse { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string currentFolderPath; protected void ClearAllFields() { listBoxFolders.Items.Clear(); listBoxFiles.Items.Clear(); textBoxFolder.Text = ""; textBoxFileName.Text = ""; textBoxCreationTime.Text = ""; textBoxLastAccessTime.Text = ""; textBoxLastWriteTime.Text = ""; textBoxFileSize.Text = ""; }//ClearAllFields protected void DisplayFileInfo(string fileFullName) { FileInfo theFile = new FileInfo(fileFullName); if (!theFile.Exists) throw new FileNotFoundException("File not found:" + fileFullName); textBoxFileName.Text = theFile.Name; textBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString(); textBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongTimeString(); textBoxFileSize.Text = theFile.Length.ToString() + "bytes"; textBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongTimeString(); }//displayFileInfo protected void DisplayFolderList(string folderFullName) { DirectoryInfo theFolder = new DirectoryInfo(folderFullName); if (!theFolder.Exists) throw new DirectoryNotFoundException("Folder not found:" + folderFullName); ClearAllFields(); textBoxFolder.Text = theFolder.FullName; currentFolderPath = theFolder.FullName; foreach (DirectoryInfo nextFolder in theFolder.GetDirectories()) listBoxFolders.Items.Add(nextFolder.Name); foreach (FileInfo nextFile in theFolder.GetFiles()) listBoxFiles.Items.Add(nextFile.Name); }//displayFolderlist protected void OnDisplayButtonClick(object sender, EventArgs e) { try { OpenFileDialog chooseFile = new OpenFileDialog(); chooseFile.FileOk += new CancelEventHandler(delegate(object ds,CancelEventArgs de) { textBoxInput.Text = chooseFile.FileName; }); chooseFile.ShowDialog(); string folderPath = textBoxInput.Text; DirectoryInfo theFolder = new DirectoryInfo(folderPath); if (theFolder.Exists) { DisplayFolderList(theFolder.FullName); return; } FileInfo theFile=new FileInfo(folderPath); if (theFile.Exists) { DisplayFolderList(theFile.Directory.FullName); int index = listBoxFiles.Items.IndexOf(theFile.Name); listBoxFiles.SetSelected(index, true); return; } throw new FileNotFoundException("There is no file or folder with this name: " + textBoxInput.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } }//OnDisplayButtonClick protected void OnListBoxFilesSelected(object sender, EventArgs e) { try { string selectedString = listBoxFiles.SelectedItem.ToString(); string fullFileName = Path.Combine(currentFolderPath, selectedString); DisplayFileInfo(fullFileName); } catch (Exception ex) { MessageBox.Show(ex.Message); } }//OnListBoxFilesSelected protected void OnListBoxFoldersSelected(object sender, EventArgs e) { try { string selectedString = listBoxFolders.SelectedItem.ToString(); string fullPathName = Path.Combine(currentFolderPath, selectedString); DisplayFolderList(fullPathName); } catch (Exception ex) { MessageBox.Show(ex.Message); } } protected void OnUpButtonClick(object sender, EventArgs e) { try { string folderPath = new FileInfo(currentFolderPath).DirectoryName; DisplayFolderList(folderPath); } catch (Exception ex) { MessageBox.Show(ex.Message); } }//OnUpButtonClick protected void OnDeleteButtonClick(object sender, EventArgs e) { try { string filePath = Path.Combine(currentFolderPath, textBoxFileName.Text); string query = "Really delete the file/n" + filePath + "?"; if (MessageBox.Show(query, "Delete file?", MessageBoxButtons.YesNo) == DialogResult.Yes) { File.Delete(filePath); DisplayFolderList(currentFolderPath); } } catch (Exception ex) { MessageBox.Show("Unable to delete file.The following exception occurred:/n " + ex.Message, "Failed"); } }//OnDeleteButtonClick protected void OnMoveButtonClick(object sender, EventArgs e) { try { string filePath = Path.Combine(currentFolderPath, textBoxFileName.Text); string query = "Really move the file/n" + filePath + "/nto " + textBoxNewPath.Text + "?"; if (MessageBox.Show(query, "Move File?", MessageBoxButtons.YesNo) == DialogResult.Yes) { File.Move(filePath, textBoxNewPath.Text); DisplayFolderList(currentFolderPath); } } catch (Exception ex) { MessageBox.Show("Unable to move file.The following exception occurred:/n" + ex.Message, "Failed"); } }//OnMoveButtonClick protected void OnCopyButtonClick(object sender, EventArgs e) { try { string filePath = Path.Combine(currentFolderPath, textBoxFileName.Text); string query = "Really copy the file/n" + filePath + "/nto " + textBoxNewPath.Text + "?"; if (MessageBox.Show(query, "Copy File?", MessageBoxButtons.YesNo) == DialogResult.Yes) { File.Copy(filePath, textBoxNewPath.Text); DisplayFolderList(currentFolderPath); } } catch (Exception ex) { MessageBox.Show("Unable to copy file.THe following exception occurred:/n" + ex.Message, "Failed"); } } } }

注册表和应用程序的独立存储空间

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Win32; using System.IO; using System.IO.IsolatedStorage; namespace Registor { public partial class Form1 : Form { //private System.Windows.Forms.ListBox listBoxMessages; //private System.Windows.Forms.Button buttonChooseColor; private ColorDialog chooseColorDialog = new ColorDialog(); public Form1() { InitializeComponent(); //buttonChooseColor.Click += new EventHandler(OnClickChooseColor); try { if (ReadSettings() == false) listBoxMessages.Items.Add("No information in registry"); else listBoxMessages.Items.Add("Information read in from registery"); StartPosition = FormStartPosition.Manual; } catch (Exception ex) { listBoxMessages.Items.Add("A problem occurred reading in data from registery:"); listBoxMessages.Items.Add(ex.Message); } } //protected override void Dispose(bool disposing) //{ // if (disposing && (components != null)) // { // components.Dispose(); // } // SaveSettings(); // base.Dispose(disposing); //} void OnClickChooseColor(object sender, EventArgs e) { if (chooseColorDialog.ShowDialog() == DialogResult.OK) BackColor = chooseColorDialog.Color; } /* void SaveSettings() { RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software", true); RegistryKey wroxKey = softwareKey.CreateSubKey("WroxPress"); RegistryKey selfPlacingWindowKey = wroxKey.CreateSubKey("SelfPlacingWindow"); selfPlacingWindowKey.SetValue("Red", (object)(int)BackColor.R); selfPlacingWindowKey.SetValue("Green", (object)(int)BackColor.G); selfPlacingWindowKey.SetValue("Blue", (object)(int)BackColor.B); selfPlacingWindowKey.SetValue("Width", (object)(int)Width); selfPlacingWindowKey.SetValue("Height", (object)(int)Height); selfPlacingWindowKey.SetValue("X", (object)DesktopLocation.X); selfPlacingWindowKey.SetValue("Y", (object)DesktopLocation.Y); selfPlacingWindowKey.SetValue("WindowState", (object)WindowState.ToString()); } */ void SaveSettings() { IsolatedStorageFile storFile = IsolatedStorageFile.GetUserStoreForDomain(); IsolatedStorageFileStream storStream = new IsolatedStorageFileStream("SelfPlacingWindow.xml", FileMode.Create, FileAccess.Write ); System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(storStream, Encoding.UTF8); writer.Formatting = System.Xml.Formatting.Indented; 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();//settings writer.WriteEndDocument(); writer.Flush(); writer.Close(); storStream.Close(); storFile.Close(); } /* bool ReadSettings() { RegistryKey softWareKey = Registry.LocalMachine.OpenSubKey("Software"); RegistryKey wroxKey = softWareKey.OpenSubKey("WroxPress"); if (wroxKey == null) return false; RegistryKey selfPlacingWindowKey = wroxKey.OpenSubKey("SelfPlacingWindow"); if (selfPlacingWindowKey == null) return false; else listBoxMessages.Items.Add("Successfully opened key: " + selfPlacingWindowKey.ToString()); int redComponent = (int)selfPlacingWindowKey.GetValue("Red"); int greenComponent = (int)selfPlacingWindowKey.GetValue("Green"); int blueComponent = (int)selfPlacingWindowKey.GetValue("Blue"); this.BackColor = Color.FromArgb(redComponent, greenComponent, blueComponent); listBoxMessages.Items.Add("Background color: " + BackColor.Name); int X = (int)selfPlacingWindowKey.GetValue("X"); int Y = (int)selfPlacingWindowKey.GetValue("Y"); this.DesktopLocation = new Point(X, Y); listBoxMessages.Items.Add("Desktop location: " + DesktopLocation.ToString()); this.Height = (int)selfPlacingWindowKey.GetValue("Height"); this.Width = (int)selfPlacingWindowKey.GetValue("Width"); listBoxMessages.Items.Add("Size: " + new Size(Width, Height).ToString()); string initialWindowState = (string)selfPlacingWindowKey.GetValue("WindowState"); listBoxMessages.Items.Add("Window State: " + initialWindowState); this.WindowState = (FormWindowState)FormWindowState.Parse(WindowState.GetType(), initialWindowState); return true; } */ bool ReadSettings() { IsolatedStorageFile storFile = IsolatedStorageFile.GetUserStoreForDomain(); string[] userFiles = storFile.GetFileNames("SelfPlacingWindow.xml"); foreach (string userFile in userFiles) { 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 "Width": Width = int.Parse(reader.ReadString()); break; case "Height": 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()); listBoxMessages.Items.Add("Window State: " + WindowState.ToString()); storStream.Close(); storFile.Close(); return true; } return false; } } }

stream fileStreamstringReader textReader stringWriter textWriter

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace TextReador { public partial class BinaryFileReader : Form { private OpenFileDialog chooseOpenFileDialog = new OpenFileDialog(); private string chosenFile; public BinaryFileReader() { InitializeComponent(); openFile.Click += new EventHandler(OnFileOpen); chooseOpenFileDialog.FileOk += new CancelEventHandler(OnOpenFileDialogOK); } void OnFileOpen(object sender, EventArgs e) { chooseOpenFileDialog.ShowDialog(); } void OnOpenFileDialogOK(object sender, CancelEventArgs e) { chosenFile = chooseOpenFileDialog.FileName; this.Text = Path.GetFileName(chosenFile); DisplayFile(); } void DisplayFile() { int nCols = 16; FileStream inStream = new FileStream(chosenFile, FileMode.Open, FileAccess.Read); long nBytesToRead = inStream.Length; if (nBytesToRead > 65536 / 4) nBytesToRead = 65536 / 4; int nLines = (int)(nBytesToRead / nCols) + 1; string[] lines = new string[nLines]; int nBytesRead = 0; for (int i = 0; i < nLines; i++) { StringBuilder nextLine = new StringBuilder(); nextLine.Capacity = 4 * nCols; for (int j = 0; j < nCols; j++) { int nextByte = inStream.ReadByte(); nBytesRead++; if (nextByte < 0 || nBytesRead > 65536) break; char nextChar = (char)nextByte; if (nextChar < 16) nextLine.Append(" x0 " + string.Format("{0,1:X}", (int)nextChar)); else if (char.IsLetterOrDigit(nextChar) || char.IsPunctuation(nextChar)) nextLine.Append(" " + nextChar + " "); else nextLine.Append(" x " + string.Format("{0,2:x}", (int)nextChar)); } lines[i] = nextLine.ToString(); } inStream.Close(); this.textBoxContents.Lines = lines; } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值