标签:
class Program
{
static void Main(string[] args)
{
FileInfo file = new FileInfo(@"E:\学习笔记\C#平台\test.txt");//创建文件
Console.WriteLine("创建时间:" + file.CreationTime);
Console.WriteLine("路径:" + file.DirectoryName);
StreamWriter sw = file.AppendText();//打开追加流
sw.Write("李志伟");//追加数据
sw.Dispose();//释放资源,关闭文件
File.Move(file.FullName, @"E:\学习笔记\test.txt");//移动
Console.WriteLine("完成!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
//创建文件夹
DirectoryInfo directory = new DirectoryInfo(@"E:\学习笔记\C#平台\test");
directory.Create();
Console.WriteLine("父文件夹:" + directory.Parent.FullName);
//输出父目录下的所有文件与文件夹
FileSystemInfo[] files = directory.Parent.GetFileSystemInfos();
foreach (FileSystemInfo fs in files)
{
Console.WriteLine(fs.Name);
}
Directory.Delete(directory.FullName);//删除文件夹
Console.WriteLine("完成!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Path.Combine(@"E:\学习笔记\C#平台", @"Test\Test.TXT"));//连接
Console.WriteLine("平台特定的字符:" + Path.DirectorySeparatorChar);
Console.WriteLine("平台特定的替换字符:" + Path.AltDirectorySeparatorChar);
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives)
{
if (d.IsReady)
{
Console.WriteLine("总容量:" + d.TotalFreeSpace);
Console.WriteLine("可用容量:" + d.AvailableFreeSpace);
Console.WriteLine("驱动器类型:" + d.DriveFormat);
Console.WriteLine("驱动器的名称:" + d.Name + "\n");
}
}
Console.WriteLine("OK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
string path = @"E:\学习笔记\C#平台\Test.txt";
File.WriteAllText(path, "测试数据");
Console.WriteLine("文件已创建,请查看!");
Console.ReadLine();
File.Move(path, @"E:\学习笔记\Test.txt");
Console.WriteLine("移动完成,请查看!");
Console.ReadLine();
File.Copy(@"E:\学习笔记\Test.txt", path);
Console.WriteLine("文件已复制,请查看!");
Console.ReadLine();
File.Delete(path);
File.Delete(@"E:\学习笔记\Test.txt");
Console.WriteLine("文件已删除,请查看!\nOK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
IsFile(@"E:\学习笔记\C#平台\Test.txt");
IsFile(@"E:\学习笔记\");
IsFile(@"E:\学习笔记\XXXXXXX");
Console.Read();
}
//判断路径是否是文件或文件夹
static void IsFile(string path)
{
if (Directory.Exists(path))
{
Console.WriteLine("是文件夹!");
}
else if (File.Exists(path))
{
Console.WriteLine("是文件!");
}
else
{
Console.WriteLine("路径不存在!");
}
}
}
class Program
{
static void Main(string[] args)
{
string path = @"E:\学习笔记\C#平台\Test.txt";
byte[] b = File.ReadAllBytes(path);
Console.WriteLine("ReadAllBytes读二进制:");
foreach (byte temp in b)
{
Console.Write((char)temp+" ");
}
string[] s = File.ReadAllLines(path, Encoding.UTF8);
Console.WriteLine("\nReadAllLines读所有行:");
foreach (string temp in s)
{
Console.WriteLine("行:"+temp);
}
string str = File.ReadAllText(path, Encoding.UTF8);
Console.WriteLine("ReadAllText读所有行:\n" + str);
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
string path = @"E:\学习笔记\C#平台\Test.txt";
File.WriteAllBytes(path,new byte[] {0,1,2,3,4,5,6,7,8,9});//写入二进制
Console.WriteLine("WriteAllBytes写入二进制成功");
Console.ReadLine();
string[] array = {"123","456","7890"};
File.WriteAllLines(path, array, Encoding.UTF8);//写入所有行
Console.WriteLine("WriteAllLines写入所有行成功");
Console.ReadLine();
File.WriteAllText(path, "abcbefghijklmn",Encoding.UTF8);//写入字符串
Console.WriteLine("WriteAllText写入字符串成功\nOK!");
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
string path = @"E:\学习笔记\C#平台\Test.txt";
//以写文件的方式创建文件
FileStream file = new FileStream(path, FileMode.CreateNew, FileAccess.Write);
string str = "测试文件--李志伟";
byte[] bytes = Encoding.Unicode.GetBytes(str);
file.Write(bytes, 0, bytes.Length);//写入二进制
file.Dispose();
Console.WriteLine("写入数据成功!!!");
Console.ReadLine();
//以读文件的方式打开文件
file = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] temp = new byte[bytes.Length];
file.Read(temp, 0, temp.Length);//读取二进制
Console.WriteLine("读取数据:" + Encoding.Unicode.GetString(temp));
file.Dispose();
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
string path = @"E:\学习笔记\C#平台\Test.txt";
//以写文件的方式创建文件
FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(file);
sw.WriteLine("测试文件--李志伟");
sw.Dispose();
Console.WriteLine("写入数据成功!!!");
Console.ReadLine();
//以读文件的方式打开文件
file = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(file);
Console.WriteLine("读取数据:"+sr.ReadToEnd());
sr.Dispose();
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
MemoryMappedFile mmfile = MemoryMappedFile.CreateFromFile(@"E:\Test.txt", FileMode.OpenOrCreate, "MapName", 1024 * 1024);
MemoryMappedViewAccessor view = mmfile.CreateViewAccessor();//内存映射文件的视图
//或使用数据流操作内存文件
//MemoryMappedViewStream stream = mmfile.CreateViewStream();
string str = "测试数据:李志伟!";
int length = Encoding.UTF8.GetByteCount(str);
view.WriteArray<byte>(0, Encoding.UTF8.GetBytes(str), 0, length);//写入数据
byte[] b = new byte[length];
view.ReadArray<byte>(0, b, 0, b.Length);
Console.WriteLine(Encoding.UTF8.GetString(b));
mmfile.Dispose();//释放资源
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
FileStream file = new FileStream(@"E:\Test.txt", FileMode.Open, FileAccess.Read);
FileSecurity filesec = file.GetAccessControl();//得到文件访问控制属性
//输出文件的访问控制项
foreach (FileSystemAccessRule filerule in filesec.GetAccessRules(true, true, typeof(NTAccount)))
{
Console.WriteLine(filerule.AccessControlType + "--" + filerule.FileSystemRights + "--" + filerule.IdentityReference);
}
file.Dispose();
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
DirectoryInfo dir= new DirectoryInfo(@"E:\学习笔记\C#平台");
DirectorySecurity filesec = dir.GetAccessControl();//得到文件访问控制属性
//输出文件的访问控制项
foreach (FileSystemAccessRule filerule in filesec.GetAccessRules(true, true, typeof(NTAccount)))
{
Console.WriteLine(filerule.AccessControlType + "--" + filerule.FileSystemRights + "--" + filerule.IdentityReference);
}
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
FileStream file = new FileStream(@"E:\Test.txt", FileMode.Open, FileAccess.Read);
FileSecurity filesec = file.GetAccessControl();//得到文件访问控制属性
Print(filesec.GetAccessRules(true, true, typeof(NTAccount)));//输出文件访问控制项
FileSystemAccessRule rule = new FileSystemAccessRule(
new NTAccount(@"CENTER-YFB-512\LiZW"), //计算机账户名
FileSystemRights.Delete, //操作权限
AccessControlType.Allow);//能否访问受保护的对象
filesec.AddAccessRule(rule);//增加ACL项
Print(filesec.GetAccessRules(true, true, typeof(NTAccount)));//输出文件访问控制项
filesec.RemoveAccessRule(rule);//移除ACL项
Print(filesec.GetAccessRules(true, true, typeof(NTAccount)));//输出文件访问控制项
file.Dispose();
Console.Read();
}
//输出文件访问控制项
static void Print(AuthorizationRuleCollection rules)
{
foreach (FileSystemAccessRule filerule in rules)
{
Console.WriteLine(filerule.AccessControlType + "--" + filerule.FileSystemRights + "--" + filerule.IdentityReference);
}
Console.WriteLine("================================================");
}
}
class Program
{
static void Main(string[] args)
{
string path = @"SOFTWARE\Microsoft\Internet Explorer\Extensions";
RegistryKey pregkey = Registry.LocalMachine.OpenSubKey(path, true);//以只读方式
if (pregkey != null)
{
Console.WriteLine(pregkey.Name + "--" + pregkey.SubKeyCount + "--" + pregkey.ValueCount);
string preName = System.Guid.NewGuid().ToString();
pregkey.CreateSubKey(preName);//增加一个子键
RegistryKey new_pregkey = Registry.LocalMachine.OpenSubKey(path + @"\" + preName, true);
new_pregkey.SetValue("姓名", "李志伟");//设置一个键的键值
new_pregkey.SetValue("键名", "值内容");//设置一个键的键值
Console.WriteLine(pregkey.Name + "--" + pregkey.SubKeyCount + "--" + pregkey.ValueCount);
pregkey.Close();
new_pregkey.Close();
}
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
//写文件
IsolatedStorageFileStream storStream = new IsolatedStorageFileStream(@"Test.txt", FileMode.Create, FileAccess.Write);
string str = "测试数据:李志伟!ABCD";
byte[] bs = Encoding.UTF8.GetBytes(str);
storStream.Write(bs, 0, bs.Length);//写数据
storStream.Dispose();
//读文件
IsolatedStorageFile storFile = IsolatedStorageFile.GetUserStoreForDomain();
string[] files=storFile.GetFileNames(@"Test.txt");
foreach (string t in files)
{
Console.WriteLine(t);
storStream = new IsolatedStorageFileStream(t, FileMode.Open, FileAccess.Read);
StreamReader sr=new StreamReader(storStream);
Console.WriteLine("读取文件:"+sr.ReadToEnd());
sr.Dispose();
storFile.DeleteFile(t);//删除文件
}
storFile.Dispose();
Console.WriteLine("OK!");
Console.Read();
}
}
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4315716.html