【问题标题】:Trouble getting my button to call my class无法让我的按钮呼叫我的班级
【发布时间】:2010-06-28 14:17:41
【问题描述】:

我有一个 Windows 窗体应用程序。该应用程序的作用是让用户浏览到他们希望为其重命名文件的驱动器/文件夹。此应用程序重命名具有“无效”字符(在 RegEx 模式中定义)的文件。

我想在这里发生的是,在用户决定使用哪个驱动器/文件夹后,会弹出一个 datagridview 显示驱动器/文件夹中将要重命名的用户文件。然后用户单击一个按钮来实际重命名文件。我在设置 DriveRecursion_Results.cs 中的按钮代码时遇到了问题。有谁能够帮我?代码请 - 我对此非常陌生,需要查看语法才能理解。

Form1代码:

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

            }

        }

        }

    }
}
    }

感谢任何帮助!

谢谢:)

【问题讨论】:

  • 问题只是调用静态方法吗?
  • 这就是问题所在。我想从我的 DriveRecursion 方法中调用 filePath - 但我不确定如何。我需要将它传递到我的按钮中,以便它知道对文件路径进行文件清理。

标签: c#


【解决方案1】:

 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

【讨论】:

  • 我对此进行了调试,在 Main 中声明 filePath 对我来说毫无意义。它仍然为空。 filePath 的目的是为 FileCleanup 提供路径,以便它可以重命名文件。至于 dgr2——它是我的 datagridview 的另一个实例。我不确定这是否有必要,但我有它,以防用户指定的驱动器/文件夹中没有任何东西需要清理。
  • 您必须将 filePath 声明为类成员,正如我在上面所写的。这样做的原因是,您一次创建列表,而您在不同时间(按下按钮时)进行实际清理。因此,数据必须同时永久存储在某个地方。如果它仍然为 null,这可能是因为您没有在 DriveRecursion() 方法中设置它,或者您可能已声明了两次。
猜你喜欢
  • 2015-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多