变量准备
| var filePath = @"c:\doc\1.txt"; |
| var dirPath1 = @"c:\music\"; |
| var dirPath2 = @"c:\movie"; |
路径拼接
| Console.WriteLine(Path.Combine(dirPath1, "a.mp3")) |
| Console.WriteLine(Path.Combine(dirPath2, "b.mp4")) |
获取文件夹的名字
| Console.WriteLine(Path.GetDirectoryName(dirPath1)); |
| Console.WriteLine(Path.GetDirectoryName(dirPath2)); |
| Console.WriteLine(Path.GetDirectoryName(filePath)); |
| |
| Console.WriteLine(new DirectoryInfo(dirPath1).Name); |
| Console.WriteLine(new DirectoryInfo(dirPath2).Name); |
| Console.WriteLine(new DirectoryInfo(@"d:\a\b\c\d").Parent.Name); |
获取文件名或后缀
| Console.WriteLine(Path.GetFileName(filePath)); |
| Console.WriteLine(Path.GetFileNameWithoutExtension(filePath)); |
| Console.WriteLine(Path.GetExtension(filePath)); |
获取程序当前目录
| System.AppDomain.CurrentDomain.BaseDirectory |
| |
对于 aspnet core 打包成单个文件的exe
| System.Environment.CurrentDirectory |
当前程序路径
| System.Reflection.Assembly.GetExecutingAssembly().Location |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步