[英]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.