StringBuilder sb = new StringBuilder();
String[] drives = Environment.GetLogicalDrives();
sb.AppendLine("本机逻辑驱动器:" + String.Join(", ", drives));
sb.AppendLine("操作系统版本:" + Environment.OSVersion.VersionString);
sb.AppendLine("是否为64位系统:" + Environment.Is64BitOperatingSystem);
sb.AppendLine("计算机名:" + Environment.MachineName);
sb.AppendLine("处理器个数:" + Environment.ProcessorCount);
sb.AppendLine("系统启动后经过的毫秒数:" + Environment.TickCount);
sb.AppendLine("登录用户名:" + Environment.UserName);
Console.WriteLine(sb.ToString());
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{Console.WriteLine("Drive {0}", d.Name);Console.WriteLine("文件类型: {0}", d.DriveType);if (d.IsReady == true){Console.WriteLine("卷标: {0}", d.VolumeLabel);Console.WriteLine("文件系统: {0}", d.DriveFormat);Console.WriteLine("当前用户可用空间:{0} bytes", d.AvailableFreeSpace);Console.WriteLine("总可用空间:{0} bytes", d.TotalFreeSpace);Console.WriteLine("驱动器总容量:{0} bytes ", d.TotalSize);}
}
string path1 = @"c:\temp\MyTest1.txt";
if (File.Exists(path1))
{Console.WriteLine("存在 {0}文件", path1);
}
else
{Console.WriteLine("不存在 {0}文件", path1);
}
public static void Copy (string sourceFileName, string destFileName, bool overwrite)
string path1 = @"c:\temp\MyTest1.txt";
if (!File.Exists(path1))
{File.WriteAllText(path1, "OK");
}
string path2 = @"c:\temp\MyTest2.txt";
File.Copy(path1, path2, true);
- 在这段代码中,如果目标文件已存在,就直接覆盖。实际应用时,一般会先询问用户是否覆盖目标文件,然后再根据用户的选择决定是否覆盖目标文件。
删除文件
移动文件
判断某个路径是目录还是文件
if ((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory)
{Console.WriteLine("{0}是目录", path);
}
else
{Console.WriteLine("{0}是文件", path);
}