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



我有一个Path;

\\username-txdlocationConfigurationMediaManagerConfigWeb.config

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

\\username-txdlocationConfigurationWeb.config

有人能帮我写代码吗?因为我是C#的新手

DirectoryInfo.Parent返回此MediaManagerConfig,您可以执行类似的小比特字符串手动操作;

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

结果将是;

\username-txdlocationConfigurationWeb.config

如果您想要基于结果的4反斜杠,您也可以替换为\\\

您可以使用File.Copy来复制文件。要获得您的目标文件名,您可以执行

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

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

我会像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));

相关内容

最新更新