其他分享
首页 > 其他分享> > 修改文件名字或者将文件移动打新的目录

修改文件名字或者将文件移动打新的目录

作者:互联网

 1 static void Main(string[] args)
 2 {
 3     //当前工作目录
 4     Console.WriteLine($"CurrentDirectory = {Environment.CurrentDirectory}");
 5 
 6     //原始名字
 7     string oldFullPath = Path.Combine(Environment.CurrentDirectory, "Test.txt");
 8     Console.WriteLine($"path = {oldFullPath}");
 9 
10     //保持目录不变修改名字
11     //string newFullPath = Path.Combine(Environment.CurrentDirectory, "NewTest.txt");
12 
13     //移动到新的目录
14     var parentDir = Directory.GetParent(Environment.CurrentDirectory);
15     string newFullPath = Path.Combine(parentDir.FullName, "NewTest.txt");
16 
17     FileInfo fi = new FileInfo(oldFullPath);
18     fi.MoveTo(newFullPath);
19     Console.ReadKey();
20 }

 

标签:文件,Console,string,CurrentDirectory,打新,Environment,oldFullPath,txt,目录
来源: https://www.cnblogs.com/luguoshuai/p/15856618.html