C# 将参数传递给C中的类时出现问题#

C# 将参数传递给C中的类时出现问题#,c#,parameters,C#,Parameters,我在将这个参数传递给一个类时遇到了一些问题。有人有什么想法吗 第1类的代码: public void DriveRecursion(string retPath) { //recurse through files. Let user press 'ok' to move onto next step // string[] files = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectorie

我在将这个参数传递给一个类时遇到了一些问题。有人有什么想法吗

第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);

    }
}

但是,我在将文件名传递到我的SanitizeFileNames类时遇到了问题。有人能帮我吗?

参数类型应该是某种类型的可枚举集合:列表或数组就可以了。此外,字符串是不可变的,因此您可以返回已清理文件名的列表:

  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;
                }

            }
        }
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;
    }
}
public-class-SanitizeFilenames
{
公共列表文件清理(IEnumerable文件名)
{
var cleanedFileNames=新列表();
var invalidChars=Path.GetInvalidFileNameChars();
foreach(文件名中的字符串文件)
{
if(file.IndexOfAny(invalidChars)!=-1)
{
//清除文件名并将其添加到cleanedFileNames列表中
}
其他的
{
//这里没什么要打扫的
清除文件名。添加(文件);
}
}
返回清除的文件名;
}
}

参数类型应该是某种类型的可枚举集合:列表或数组就可以了。此外,字符串是不可变的,因此您可以返回已清理文件名的列表:

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;
    }
}
public-class-SanitizeFilenames
{
公共列表文件清理(IEnumerable文件名)
{
var cleanedFileNames=新列表();
var invalidChars=Path.GetInvalidFileNameChars();
foreach(文件名中的字符串文件)
{
if(file.IndexOfAny(invalidChars)!=-1)
{
//清除文件名并将其添加到cleanedFileNames列表中
}
其他的
{
//这里没什么要打扫的
清除文件名。添加(文件);
}
}
返回清除的文件名;
}
}

我想您想向FileCleanup函数传递一个脏名称,然后得到一个清除。以下是如何做到这一点:

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

    ...

    return cleanName;
}

您将注释放在哪里。

我想您希望向FileCleanup函数传递一个脏名称并获得一个清除。以下是如何做到这一点:

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

    ...

    return cleanName;
}
你把评论放在哪里

  • 您可以创建第三个类静态类,并添加名为files“
    publicstaticlist files=newlist()
    ”的静态变量作为示例
  • 创建文件时,将相同的文件添加到静态变量
  • 清理文件时,循环抛出静态变量,最后清除它
  • 您可以创建第三个类静态类,并添加名为files“
    publicstaticlist files=newlist()
    ”的静态变量作为示例
  • 创建文件时,将相同的文件添加到静态变量
  • 清理文件时,循环抛出静态变量,最后清除它

  • “有麻烦”。什么样的麻烦?不能编译吗?运行时异常?其他错误?我编辑以显示我尝试的内容。添加不是一个选项——它不在intellisense中,而且我会出错。我需要找到一种方法将这个参数传递到我的FileCleanup方法。@yeah:你把自己弄糊涂了。“
    fileNames
    包含一个文件名。请将其重命名为”
    filename
    “。然后您将看到您试图将多个文件名传递到
    SanitizeFileNames.FileCleanup
    类中,并且只需要一个@是的:事实上,使用糟糕的术语会让你自己更加困惑:你不会将参数传递给类。将参数传递给类的方法“遇到问题”。什么样的麻烦?不能编译吗?运行时异常?其他错误?我编辑以显示我尝试的内容。添加不是一个选项——它不在intellisense中,而且我会出错。我需要找到一种方法将这个参数传递到我的FileCleanup方法。@yeah:你把自己弄糊涂了。“
    fileNames
    包含一个文件名。请将其重命名为”
    filename
    “。然后您将看到您试图将多个文件名传递到
    SanitizeFileNames.FileCleanup
    类中,并且只需要一个@是的:事实上,使用糟糕的术语会让你自己更加困惑:你不会将参数传递给类。将参数传递给类的方法。new SanitizeFileNames.FileCleanup(文件名);给了我一个错误:FileCleanup(string)是一个“方法”,但像“类型”一样使用。我是否需要以某种方式编辑SanitizeFileNames的方法或类?它应该是新的SanitizeFileNames().FileCleanup(文件名)。(注意SanitizeFileNames后面的括号。)啊,是的,我错了。没有注意到括号。多谢各位!新的SanitizeFileNames.FileCleanup(文件名);给了我一个错误:FileCleanup(string)是一个“方法”,但像“类型”一样使用。我是否需要以某种方式编辑SanitizeFileNames的方法或类?它应该是新的SanitizeFileNames().FileCleanup(文件名)。(注意SanitizeFileNames后面的括号。)啊,是的,我错了。没有注意到括号。多谢各位!正如你所说,它应该是一个“某种类型”的集合,所以实际上,它应该是一个“某种类型”的序列,所以让我们把它变成一个IEnumerable,而不是一个列表。正如你所说,它应该是一个“某种类型”的集合,所以请让它成为一个“某种类型”的序列因此,让我们将其设置为IEnumerable,而不是列表。
    String cleanName = new SanitizeFileNames().FileCleanup(fileNames);