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 6724021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:38:18+00:00 2026-05-26T09:38:18+00:00

I have a simple foreach here where I am have got the folder location

  • 0

I have a simple foreach here where I am have got the folder location using the folderBrowserDialog tool and am now trying to go through each one of the files and replace certain characters within the file name.

I am getting an error which says it cannot find the file when it gets to this part of the code File.Move(_FileName, _NewFileName);

Can anyone shed any light on this? Would be much appreciated.

Thanks

foreach (FileInfo Files in Folder.GetFiles())
            {
                _FileName = Files.Name;

                _NewFileName = _FileName.Replace(" ", "-").Replace(",", "-");

                File.Move(_FileName, _NewFileName);

                File.Delete(_FileName);

            }
  • 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-26T09:38:19+00:00Added an answer on May 26, 2026 at 9:38 am

    You need to use Files.FullName not Files.Name

    FullName includes the full path (i.e. C:\test\foo.txt) which is needed by File.Move() and File.Delete() while Name is just the file name itself (i.e. foo.txt).

    Edit:

    @crashmstr is correct you should not do a string replace on the full path. All in all I’d probably do it this way:

    foreach (FileInfo file in Folder.GetFiles())
    {
        string originalFileName = file.FullName;
        string fileName = file.Name.Replace(" ", "-").Replace(",", "-");
        string newFileName = Path.Combine(file.DirectoryName, fileName);
    
        File.Move(originalFileName, newFileName);
    }
    

    Also keep in mind File.Delete() is not needed here, since the original file won’t be there anymore after you move it.

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

Sidebar

Related Questions

I have a pretty simple case which I started solving using foreach(), but then
I got into a dilemma about using has_many, :through. Suppose I am trying to
I have a very simple query $this->db->select('id, title'); $this->db->order_by('id', 'DESC'); $query = $this->db->get('journal'); foreach($query->result()
I am building a Space Invaders game. I have got a simple algorithm that
I have been trying to use one View for updating an object and all
Hi I'm trying to create a simple registration form with ajax validation. I've got
I've got a simple routine that populates a record in a table. I have
I have simple regex \.*\ for me its says select everything between and ,
I have simple win service, that executes few tasks periodically. How should I pass
i have simple regular expression: ^123$ Matches are for example 123 1234 etc. How

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.