2012-02-02 26 views
0
string EV_HOME = System.Environment.GetEnvironmentVariable("EV_HOME"); 
    string myFilePath = EV_HOME + "\\MyFolder\\MySubFolder"; 

假設EV_HOME回報下的子文件夾:\ myprogram \ leanrning \ anotherfolder什麼是更好的方式來去除附着在文件路徑C#2.0

我怎樣才能刪除該文件夾anotherfolder並獲得myFilePath因爲這

C:\ myprogram \ leanrning \ MyFolder文件\ MySubFolder

我所知道的是循環的Ev_ HOME值並將每個(除最後一個)作爲新字符串構建。

謝謝。

回答

1
using System; 
using System.Collections.Generic; 
using System.Text; 

namespace ConsoleApplication12 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string EV_HOME = @"C:\myprogram\leanrning\anotherfolder\"; 
      string parentFolder = new System.IO.DirectoryInfo(EV_HOME).Parent.FullName; 
      string myFilePath = parentFolder + "\\MyFolder\\MySubFolder"; 
      Console.WriteLine(myFilePath); 
     } 
    } 
} 
+0

哇,'Parent.FullName' +1。謝謝。 – 2012-02-02 01:42:25

+0

錯誤在很多層面上。 1)不要硬編碼系統目錄的路徑(即使在環境變量中)。 2)不要自己連接路徑或假定目錄分隔符是什麼。使用爲此目的明確設計的['Path.Combine'方法](http://msdn.microsoft.com/zh-cn/library/system.io.path.combine.aspx)。 – 2012-02-02 03:25:33

相關問題