Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 888473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:24:14+00:00 2026-05-15T13:24:14+00:00

I have a Windows Form application. What this application does, is let the user

  • 0

I have a Windows Form application. What this application does, is let the user browse to a drive/folder they wish to have files renamed for. This app renames files that have “invalid” characters (that are defined in a RegEx pattern).

What i want to happen here is, after the user decides which drive/folder to use, a datagridview pops up showing the user files in the drive/folder that are going to be renamed. The user then clicks a button to actually rename the files. I’m having trouble though getting the code for my button in DriveRecursion_Results.cs set up. Can anybody help me? Code plz — i’m extremely new to this and need syntax to look at to understand.

Form1 code:

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 code: [the button is in here that i need help with!]

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 code:

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

            }

        }

        }

    }
}
    }

Any help is appreciated!

Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:24:15+00:00Added an answer on May 15, 2026 at 1:24 pm

    Put

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

    and in driveRecursion method, just use

    filePath = new List<string>();
    

    and in the action button method, why don’t you do

      if(filePath != null)
            SanitizeFileNames.FileCleanup(filePath);
    
    1. You call filePath.Add twice ?

    2. Your ‘else‘ is in the wrong place too.

    3. What is dgr2?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# Windows Form application that contains a menu with this event:
I have a windows form without running it in an application. this is the
I am using C# with a Windows Application Form. In this I have a
I have a windows form application. On the main form a user will enter
I have a windows form application. On the form there are three groupboxs. Each
I have a Windows Form application. Version 1 of my app has been released
I have a Windows form application that's consuming web services over https and is
I have a Windows Form application with a button named loginBtn and a label
I have a C# windows form application (Actually it is a IE toolbar application),
I have a C# Windows Form Application and a live video feed. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.