繁体   English   中英

在同一文件夹/目录位置的一个位置上创建文件副本

[英]Creating copy of a file at one position up in the same folder/directory location

我有一条路;

\\\\username-txd\location\Configuration\MediaManagerConfig\Web.config

我想在同一文件夹中的某个位置创建文件副本,即

\\\\username-txd\location\Configuration\Web.config

由于我是C#的新手,因此谁能帮助我提供代码?

您可以使用File.Copy复制文件。 要获取目标文件名,您可以

Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(path)), Path.GetFileName(path));

'path'包含文件名的完整路径。 您需要导入System.IO

DirectoryInfo.Parent返回此MediaManagerConfig ,您可以执行一点点字符串操作,例如;

var di = new DirectoryInfo(@"\\\\username-txd\location\Configuration\MediaManagerConfig\Web.config");
Console.WriteLine(di.FullName.Replace(di.Parent.Name + Path.DirectorySeparatorChar, ""));

结果将是;

\\username-txd\location\Configuration\Web.config

如果要根据结果获得4个反斜杠,也可以用\\\\替换为\\\\\\\\

我会像DirectoryInfo类的功能一样使用seomthing。 它知道文件系统上的关系,并提供例如.Parent属性:

string originalFilename = "\\\\username-txd\\location\\Configuration\\MediaManagerConfig\\Web.config";
string originalPath = Path.GetDirectoryName(originalFilename);
string newPath = Path.Combine(new DirectoryInfo(originalPath).Parent.FullName, Path.GetFileName(originalFilename));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM