2011-06-01 131 views
-3
  1. 我在閱讀文件和子文件夾時遇到問題。我的代碼對於給定的固定源代碼路徑很好,E:\\Folder\\test\\test2如何從文件夾和子文件夾中提取數據文件

  2. 測試中有很多文件夾,如test2,test3,test4等。我想提取主文件夾test中的數據文件。

  3. 例如,我想提取測試中的文件,所以我想讀取測試中包含的所有文件,而不是爲test3,test4和許多代碼編寫代碼。我想提取所有文件並將其寫入另一個驅動器上的相同源結構。

  4. 一樣,如果源結構是這樣E:\\Folder\\test\\test2則目標結構應該像C:\\Folder\\test\\test2

有沒有辦法做到這一點?

這裏是我的代碼,

private void dateTimePicker1_ValueChanged(object sender, EventArgs e) 
{ 
    DateTime dt = dateTimePicker1.Value; 
    txtSelectedDate.Text = dt.ToString("yyyyMMdd"); 
    selectedDate = txtSelectedDate.Text; 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    DateTime stdate = Datetimepicker1.value; 
    while (stdate <= DateTime.Now) 
    { 
     txtSelectedDate.Text = stdate.ToString("yyyyMMdd"); 
     selectedDate = txtSelectedDate.Text; 
     string DayBgSpot = "E:\\Folder\\test\\test2"; 
     string DayBgSpotDestination = "E:\\Folder1"; 
     int DT = int.Parse(txtSelectedDate.Text); 
     FileReader Reader = new FileReader(); 
     FileReader Reader1 = new FileReader(); 
     Reader.OpenDirectory(DayBgSpot); 
     Reader.ReadNaster(); 
     string path = DayBgSpotDestination + "\\" + txtSelectedDate.Text + ".txt"; 
     StreamWriter Strwriter = new StreamWriter(path); 
     try 
     { 
      while (Reader.iMaRecordsLeft > 0) 
      { 
       string SecName = Reader.sMaSecName; 
       string Symbol = Reader.sMaSecSymbol; 
       Symbol = prefix + Symbol; 
       int abc = 0; 

       Reader.OpenSecurityByName(Reader.sMaSecName); 
       if (Reader.iSeRecords > 0) 
       { 
        while (Reader.iSeRecordsLeft > 0) 
        { 
         Reader.ReadDay(); 
         float O = Reader.dSeo; 
         float H = Reader.dSeh; 
         float L = Reader.dSel; 
         float C = Reader.dSec; 
         double V = Reader.dSeV; 
         double OI = Reader.dSrest; 
         string T = Reader.iSeTime.ToString(); 
         string D = Reader.iSeDate.ToString(); 
         if (int.Parse(D) == DT) 
         { 
          string a = string.Concat(SecName, ",", Symbol, ",", D, ",", T, ",", O, ",", H, ",", L, ",", C, ",", V, ",", OI); 
          if (SecName != "" && V != 0) 
          { 
           Strwriter.WriteLine(a); 
          } 
         } 
        } 
       } 
       abc++; 
       Reader.ReadNaster(); 
      } 

      Reader.CloseDirectory(); 
      Strwriter.Close(); 
      Strwriter.Dispose(); 
     } 
     catch 
     { 
     } 
     stdate = stdate.AddDays (1); // It will get next date till present 
    } 
} 

回答

1

喜歡的東西

System.IO.DirectoryInfo baseFolder = new DirectoryInfo(@"c:\Folder\test\"); 
string destinationPath = @"e:\Folder\test\"; 
System.IO.DirectoryInfo[] subDirs = baseFolder.GetDirectories(); 
foreach (System.IO.DirectoryInfo dirInfo in subDirs) 
{ 
    string subFolder = dirInfo.Name; 
    System.IO.FileInfo[] fileInfos = dirInfo.GetFiles("*.txt"); 
    foreach (System.IO.FileInfo fileInfo in fileInfos) 
    { 
     // Do something with the files 
     string writePath = destinationPath + subFolder + @"\" + fileInfo.Name; 
     // Write 
    } 
} 
1

如果您使用的是.NET 4.0,這是一個行:

var filepaths = Directory.GetFiles(path: @"C:\", searchPattern: "*pattern*", searchOption: SearchOption.AllDirectories); 

顯然,根路徑搜索模式與建議的樣本不符,但我的意圖應該清楚。

我希望這可以幫助

相關問題