文章目录
- 1.文件夹的操作
- 1.1Directory类的部分方法
- 1.2程序代码
- 2.Path
-
- 3.Environment
- 3.1Environment类
- 3.2SpecialFolder类
- 3.3程序代码
1.文件夹的操作
1.1Directory类的部分方法
1.2程序代码
try
{string path = @"D:\01";if (Directory.Exists(path)){string[] dirs = Directory.GetDirectories(path);Console.WriteLine("Child Contents");foreach (string dir in dirs){string[] files = Directory.GetFiles(path);Console.WriteLine("File:");foreach (string file in files)Console.WriteLine(file);}}else{Console.WriteLine("Directory do not exist ");}
}
catch (Exception ex)
{Console.WriteLine(ex.ToString());
}
2.Path
2.1Path类的部分字段和方法
2.2程序代码
static void Main(string[] args)
{Console.WriteLine("操作系统中的路径分隔符:"+Path.DirectorySeparatorChar);Console.WriteLine("操作系统中环境变量中的路径分隔符:" + Path.PathSeparator);Console.WriteLine("操作系统中的卷分隔符:" + Path.VolumeSeparatorChar);string pa = @"D:\test.txt";Console.WriteLine("指定路径的文件名:" + Path.GetFileName(pa));Console.WriteLine("指定路径的主文件名:" + Path.GetFileNameWithoutExtension(pa));Console.WriteLine("指定路径的扩展名:" + Path.GetExtension(pa));Console.WriteLine("更改指定路径中的扩展名:" + Path.ChangeExtension(pa,"cx"));Console.WriteLine("返回指定路径:" + Path.GetDirectoryName(pa));Console.WriteLine("合并两个路径字符串:" + Path.Combine(Path.GetDirectoryName(pa), Path.GetFileName(pa)));
}
3.Environment
3.1Environment类
3.2SpecialFolder类
3.3程序代码
static void Main(string[] args)
{Console.WriteLine("程序所在的目录:" + Environment.CurrentDirectory);Console.WriteLine("NetBIOS名称:" + Environment.MachineName);Console.WriteLine("环境定义的换行字符串:" + Environment.NewLine);Console.WriteLine("操作系统的标识符和版本号:" + Environment.OSVersion);Console.WriteLine("处理器数量:" + Environment.ProcessorCount);Console.WriteLine("堆栈跟踪信息:" + Environment.StackTrace);Console.WriteLine("系统所在路径:" + Environment.SystemDirectory);Console.WriteLine("系统启动后经过的毫秒数:" + Environment.TickCount);Console.WriteLine("当前用户关联的网络域名:" + Environment.UserDomainName);Console.WriteLine("当前进程是否在用户交互模式中运行:" + Environment.UserInteractive);Console.WriteLine("启动当前线程的用户:" + Environment.UserName);Console.WriteLine("公共语言运行库的版本号:" + Environment.Version);Exception cc=new Exception("好样的");//Environment.FailFast("程序又出错了");Console.WriteLine("包含当前进程的命令行参数:" + Environment.GetCommandLineArgs());foreach(string cc1 in Environment.GetCommandLineArgs()){Console.WriteLine(cc1);}Console.WriteLine("计算机中所有的逻辑驱动器的名称:" + Environment.GetLogicalDrives);foreach (string cc2 in Environment.GetLogicalDrives()){Console.WriteLine(cc2);}Console.WriteLine("我的文档:" + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));Console.WriteLine("Internet Cookies:"+Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
}