2010-06-23 67 views
0

將此參數傳遞給我的類時遇到了一些問題。有人有任何想法嗎?將參數傳遞給C中的類時遇到麻煩#

1級代碼:

public void DriveRecursion(string retPath) 
{ 
    //recurse through files. Let user press 'ok' to move onto next step   
    // string[] files = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories); 

    string pattern = " *[\\~#%&*{}/<>?|\"-]+ *"; 
    //string replacement = ""; 
    Regex regEx = new Regex(pattern); 

    string[] fileDrive = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories); 
    List<string> filePath = new List<string>(); 



    dataGridView1.Rows.Clear(); 
    try 
    { 
     foreach (string fileNames in fileDrive) 
     { 

      if (regEx.IsMatch(fileNames)) 
      { 
       string fileNameOnly = Path.GetFileName(fileNames); 
       string pathOnly = Path.GetDirectoryName(fileNames); 

       DataGridViewRow dgr = new DataGridViewRow(); 
       filePath.Add(fileNames); 
       dgr.CreateCells(dataGridView1); 
       dgr.Cells[0].Value = pathOnly; 
       dgr.Cells[1].Value = fileNameOnly; 
       dataGridView1.Rows.Add(dgr); 

       \\I want to pass fileNames to my FileCleanup Method 
       \\I tried this: 
       \\SanitizeFileNames sf = new SanitizeFileNames(); 
       \\sf.Add(fileNames); <-- this always gets an error..plus it is not an action i could find in intellisense 


      } 

      else 
      { 
       continue; 
      } 

     } 
    } 
    catch (Exception e) 
    { 
     StreamWriter sw = new StreamWriter(retPath + "ErrorLog.txt"); 
     sw.Write(e); 

    } 
} 

2級代碼:

public class SanitizeFileNames 
{ 

    public void FileCleanup(string fileNames) 
    { 
     string regPattern = " *[\\~#%&*{}/<>?|\"-]+ *"; 
     string replacement = ""; 
     Regex regExPattern = new Regex(regPattern); 
    } 

我理想中SanitizeFileNames做的是通過文件名&文件路徑做一個foreach和替換無效字符(定義我的正則表達式模式)。所以,沿着這條線:

using (StreamWriter sw = new StreamWriter(@"S:\File_Renames.txt")) 
{ 
    //Sanitize and remove invalid chars 
    foreach (string Files2 in filePath) 
    { 
     try 
     { 
      string filenameOnly = Path.GetFileName(Files2); 
      string pathOnly = Path.GetDirectoryName(Files2); 
      string sanitizedFilename = regEx.Replace(filenameOnly, replacement); 
      string sanitized = Path.Combine(pathOnly, sanitizedFilename); 
      sw.Write(sanitized + "\r\n"); 
      System.IO.File.Move(Files2, sanitized); 
     } 
     //error logging 
     catch(Exception ex) 
     { 
      StreamWriter sw2 = new StreamWriter(@"S:\Error_Log.txt"); 
      sw2.Write("ERROR LOG"); 
      sw2.WriteLine(DateTime.Now.ToString() + ex + "\r\n"); 
      sw2.Flush(); 
      sw2.Close(); 
     } 
    } 
} 

但是,我無法將fileNames傳遞到我的SanitizeFileNames類。有誰能夠幫助我?

+1

「有麻煩」。什麼樣的麻煩?無法編譯?運行時異常?其他錯誤? – Oded 2010-06-23 19:34:43

+0

我編輯顯示我試過的東西。 sf.Add不是一個選項 - 它不是intellisense,而是我收到錯誤。我需要找到一種方法將這個參數傳遞給我的FileCleanup方法。 – yeahumok 2010-06-23 19:38:57

+0

@yeah:你在迷惑自己。 「'fileNames'包含一個文件名,將它重命名爲''fileName'」,然後你會看到你正在嘗試傳入多個文件名到你的'SanitizeFileNames.FileCleanup'類中,並且它只需要一個! – 2010-06-23 19:49:37

回答

3
dataGridView1.Rows.Clear(); 
     try 
     { 
      foreach (string fileNames in fileDrive) 
      { 

       if (regEx.IsMatch(fileNames)) 
       { 
        string fileNameOnly = Path.GetFileName(fileNames); 
        string pathOnly = Path.GetDirectoryName(fileNames); 

        DataGridViewRow dgr = new DataGridViewRow(); 
        filePath.Add(fileNames); 
        dgr.CreateCells(dataGridView1); 
        dgr.Cells[0].Value = pathOnly; 
        dgr.Cells[1].Value = fileNameOnly; 
        dataGridView1.Rows.Add(dgr); 

        new SanitizeFileNames().FileCleanup(fileNames); 
       } 

       else 
       { 
        continue; 
       } 

      } 
     } 
+0

新的SanitizeFileNames.FileCleanup(fileNames); 給出我錯誤:FileCleanup(字符串)是一種'方法',但像一個'類型'使用我需要以某種方式編輯我的方法或SanitizeFileNames類? – yeahumok 2010-06-23 19:40:32

+0

它應該是新的SanitizeFileNames()。FileCleanup(fileNames)。括號後面的SanitizeFileNames。) – 2010-06-23 19:42:50

+0

啊是的,我的錯誤。沒有注意到括號。非常感謝你! – yeahumok 2010-06-23 20:25:10

1

參數類型應該是某種可枚舉集合:列表或數組可以。此外,字符串是不可變的,所以你可以回到清理的文件名列表:

public class SanitizeFilenames 
{ 
    public List<string> FileCleanUp(IEnumerable<string> filenames) 
    { 
     var cleanedFileNames = new List<string>(); 

     var invalidChars = Path.GetInvalidFileNameChars(); 
     foreach(string file in filenames) 
     { 
      if(file.IndexOfAny(invalidChars) != -1) 
      { 
       // clean the file name and add it to the cleanedFileNames list 
      } 
      else 
      { 
       // nothing to clean here 
       cleanedFileNames.Add(file); 
      } 
     } 

     return cleanedFileNames; 
    } 
} 
+0

正如你所說它應該是一個集合「的某種」索姆請實際上,它應該是「某種類型」的序列,所以讓我們將其設置爲IEnumerable 而不是列表。 – 2010-06-23 19:51:38

+0

夠公平的。我更新了我的答案。 – 2010-06-24 03:14:47

1

要一個骯髒的名稱傳遞給FileCleanup功能,並得到一個乾淨的了,我想。這裏是你如何能做到這一點:

public String FileCleanup(string fileNames) 
{ 
    string regPattern = " *[\\~#%&*{}/<>?|\"-]+ *"; 
    string replacement = ""; 
    Regex regExPattern = new Regex(regPattern); 

    ... 

    return cleanName; 
} 

,並用它在你的代碼是這樣的:

String cleanName = new SanitizeFileNames().FileCleanup(fileNames); 

,你把註釋。

1
  1. 您可以創建第三類靜態類並添加名爲文件「public static List<string> Files= new List<string>()」的靜態變量作爲示例。
  2. 當您創建文件時,將相同的文件添加到靜態變量。
  3. 當你清理文件循環時拋出靜態變量,並在最後清除它。