2010-06-23 69 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. 当你清理文件循环时抛出静态变量,并在最后清除它。