yeahumok
yeahumok

Reputation: 2952

Trouble passing a parameter to a class in C#

I'm having a bit of trouble passing this parameter to a class i have. Does anybody have any ideas?

Class 1's code:

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

    }
}

Class 2's code:

public class SanitizeFileNames
{

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

What i want to do in SanitizeFileNames is do a foreach through the FileNames & FilePath and replace invalid chars (as defined in my Regex pattern). So, something along the lines of this:

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

However, I'm having trouble passing the fileNames into my SanitizeFileNames class. Can anybody help me?

Upvotes: 0

Views: 291

Answers (4)

Adam Lear
Adam Lear

Reputation: 38778

The parameter type should be an enumerable collection of some sort: a list or an array would do. Also, strings are immutable so you could return a list of cleaned up filenames:

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

Upvotes: 1

Waleed A.K.
Waleed A.K.

Reputation: 1656

  1. You can create a third class static class and add static variable called files “public static List<string> Files= new List<string>()” as example.
  2. When you create the files add the same files to the static variable.
  3. When you clean the files loop throw the static variable, and at the end clear it.

Upvotes: 1

STO
STO

Reputation: 10658

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

            }
        }

Upvotes: 3

decyclone
decyclone

Reputation: 30840

I suppose you want to pass a dirty name to the FileCleanup function and get a clean out. Here is how you can do that :

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

    ...

    return cleanName;
}

and use it in your code like this:

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

where you put the comment.

Upvotes: 1

Related Questions