2010-06-28 88 views
1

我有一個Windows窗體應用程序。該應用程序的功能是讓用戶瀏覽到他們希望爲其重命名文件的驅動器/文件夾。此應用程序重命名具有「無效」字符(在RegEx模式中定義)的文件。無法讓我的按鈕打電話給我的班級

我想要在這裏發生的是,在用戶決定使用哪個驅動器/文件夾後,會彈出一個datagridview,顯示將要重命名的驅動器/文件夾中的用戶文件。用戶然後單擊一個按鈕以實際重命名文件。我在設置DriveRecursion_Results.cs中獲取按鈕的代碼時遇到了問題。有誰能夠幫助我?代碼plz - 我對此非常陌生,需要語法才能理解。

Form 1代碼:

namespace FileMigration2 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     FolderSelect("Please select:"); 

    } 

    public string FolderSelect(string txtPrompt) 
    { 
     //Value to be returned 
     string result = string.Empty; 

     //Now, we want to use the path information to population our folder selection initial location 
     string initialPathDir = (@"C:\"); 
     System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(initialPathDir); 
     FolderBrowserDialog FolderSelect = new FolderBrowserDialog(); 
     FolderSelect.SelectedPath = info.FullName; 
     FolderSelect.Description = txtPrompt; 
     FolderSelect.ShowNewFolderButton = true; 

     if (FolderSelect.ShowDialog() == DialogResult.OK) 
     { 
      string retPath = FolderSelect.SelectedPath; 
      if (retPath == null) 
      { 
       retPath = ""; 
      } 
      DriveRecursion_Results dw = new DriveRecursion_Results(); 
      dw.Show(); 
      dw.DriveRecursion(retPath); 

      result = retPath; 

     } 

     return result; 

    } 





    } 
} 

DriveRecursion_Results.cs代碼:[按鈕在這裏,我需要幫助!]

namespace FileMigration2 
{ 
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) 
    { 
     //What do i type in here to call my FileCleanUp method??? 
    } 




} 

SanitizeFileNames.cs代碼:

namespace FileMigration2 
{ 
    public class SanitizeFileNames 
{ 

    public static 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 

      } 

     } 

     } 

    } 
} 
    } 

任何幫助表示讚賞!

謝謝:)

+0

問題只是調用靜態方法? – 2010-06-28 14:23:52

+0

以及這裏的東西。我想從我的DriveRecursion方法調用filePath - 但我不確定如何。我需要將它傳遞給我的按鈕,以便它知道在文件路徑上執行filecleanup。 – yeahumok 2010-06-28 14:26:21

回答

2

public partial class DriveRecursion_Results : Form { 
    List<string> filePath; 

和driveRecursion方法,只需要用

filePath = new List<string>(); 

,並在操作按鈕的方法,你爲什麼不這樣做

if(filePath != null) 
     SanitizeFileNames.FileCleanup(filePath); 
  1. 您撥打filePath.Add兩次?

  2. 你的'else'也是在錯誤的地方。

  3. 什麼是dgr2

+0

我調試了這一點,它在Main中聲明filePath沒有什麼意義。它保持無效。 filePath的要點是爲FileCleanup提供路徑,以便它可以重命名文件。 至於dgr2 - 它是我的datagridview的另一個實例。我不確定這是否有必要,但我有它,以防在用戶指定的驅動器/文件夾中沒有任何內容需要清理。 – yeahumok 2010-06-28 15:18:02

+0

您必須將filePath聲明爲類成員,正如我上面所寫的那樣。原因是,您一次創建列表,並且在不同的時間(按下按鈕時)進行實際的清理。因此,數據必須存儲在某個持續存在的地方。 如果它仍然爲空,則可能是因爲您沒有將其設置在DriveRecursion()方法中,或者您可能聲明瞭它兩次。 – 2010-06-30 12:36:42

相關問題