无法将参数传递给C#中的类

时间:2010-06-23 19:31:18

标签: c# parameters

我在将此参数传递给我所拥有的类时遇到了一些麻烦。有没有人有任何想法?

Class 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中做的是通过FileNames&amp; amp; FilePath并替换无效字符(在我的Regex模式中定义)。所以,就像这样:

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类。有人能帮助我吗?

4 个答案:

答案 0 :(得分: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;
                }

            }
        }

答案 1 :(得分: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;
    }
}

答案 2 :(得分:1)

我想你想将一个脏名称传递给FileCleanup函数并进行清理。以下是如何做到这一点:

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

    ...

    return cleanName;
}

并在您的代码中使用它:

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

你把评论放在哪里。

答案 3 :(得分:1)

  1. 您可以创建第三个类静态类,并添加名为“public static List<string> Files= new List<string>()”的静态变量作为示例。
  2. 创建文件时,将相同的文件添加到静态变量中。
  3. 清理文件循环时抛出静态变量,最后清除它。