2010-06-24 100 views
0

我有一个WindowsForm,它有一个DataGridView,显示我的应用程序的输出。带按钮的类是DriveRecursion_Results.cs。我需要它,以便一旦用户按下按钮,我的SanitizeFileNames类中的方法FileCleanUp()就会被调用。我不太清楚如何做到这一点。从另一个类的按钮调用我的类和方法

下面是这两个类代码:

public partial class DriveRecursion_Results : Form 
{ 
    public DriveRecursion_Results() 
    { 
     InitializeComponent(); 

    } 

    private void listView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 

    } 

    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); 
        filePath.Add(fileNames); 
       } 

       else 
       { 
        DataGridViewRow dgr2 = new DataGridViewRow(); 
        dgr2.Cells[0].Value = "No Files To Clean Up"; 
        dgr2.Cells[1].Value = ""; 
       } 

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

     } 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 

     //i want to call SanitizeFileName's method FileCleanup here. 



    } 


} 

这里是SanitizeFileNames:

public class SanitizeFileNames 
{ 

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


     foreach (string files2 in filePath) 
     { 
      try 
      { 
       string filenameOnly = Path.GetFileName(files2); 
       string pathOnly = Path.GetDirectoryName(files2); 
       string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement); 
       string sanitized = Path.Combine(pathOnly, sanitizedFileName); 
       //write to streamwriter 
       System.IO.File.Move(files2, sanitized); 

      } 
      catch (Exception ex) 
      { 
      //write to streamwriter 

      } 

     } 

    } 

} 

回答

3
private void button1_Click(object sender, EventArgs e) 
{ 
    List<string> paths = new List<string>() 
    // initialize paths to whatever is neccessary 
    //.... 
    new SanitizeFileNames().FileCleanUp(paths) 
} 

只需要创建类的实例,并调用该方法。 然而,该方法本身不使用状态之类的,所以它可以改变一个静态方法

public static void FileCleanup(List<string>filePath) 

,你就可以调用它,而无需创建一个实例,直接从类,像这样:

SanitizeFileNames.FileCleanUp(paths) 
+0

SWeko具有正确的语法。你是在App_Code中还是在一个单独的程序集中使用SanitizeFileNames? – 2010-06-24 17:39:22

+0

但我需要在DriveRecursion路径中引用filePath,以便我的FileCleanup()方法可以执行文件重命名。我如何从其他类引用filePath? – yeahumok 2010-06-24 17:39:32

+0

@yeahumok - 调用者(按钮点击事件)必须将其传入。DriveRecursion如何调用?由于click事件处理程序位于同一个类中,因此它也应该有权访问路径信息。 – GalacticCowboy 2010-06-24 17:45:23

相关问题