我有一个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));