I'm working on a windows application using C# on Microsoft Visual C# 2010.

The application is to help the user change the names of multiple files in a directory at once. For example:

I have five files in a directory c:/temp named:

rainmoon.txt

terrain.txt

things.txt

pipe.txt

temp.txt

If I try to rename "rainmoon.txt" to say, "rainsun.txt", then I just have to type that I only want to replace "moon" with "sun". My application is in such a way that it will browse through all the files that contains "moon" and then proceed to replace it with "sun"

The problem comes when both a file name (temp.txt) and a folder (also named temp) that the file is in have the same name. So for the example given above, if I try to rename **even** part of "temp.txt", I get a DirectoryNotFoundException.

Below is the code that I used. I've tried my best to troubleshoot this area, and I strongly feel it is due to File.Move. 

```
 for (int x = 0; x <= filesCheckList.CheckedItems.Count - 1; x++)
                        {
                            globalvar.fName = filesCheckList.CheckedItems[x].ToString();

                            DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);

                            FileInfo[] filesx = mydir.GetFiles(".", SearchOption.AllDirectories);

                            FileInfo[] f = mydir.GetFiles();

                            foreach (FileInfo file in filesx)
                            {
                                //try
                                {
                                    if (file.Name == globalvar.fName)
                                    {
                                        if (file.Name != "History.txt")
                                            File.Move(file.FullName, file.FullName.ToString().Replace(globalvar.FIL, globalvar.REP));
                                    }
                                }
                            }
                        }

```

My full code is below in case anyone wants to take a look. I won't rule out that maybe somewhere in the coding is causing this issue. 

```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TOMOD_W_A
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            applyButton.Enabled = false;     
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            globalvar.FIL = filTxt.Text;
            globalvar.REP = repTxt.Text;

            if (string.IsNullOrWhiteSpace(dirTxt.Text))
                MessageBox.Show("Please Key in a Directory");
            else if (globalvar.FIL == String.Empty)
                MessageBox.Show("Please give a file name");
            else
            {
                try
                {
                    if (filesCheckList.CheckedItems.Count != 0)
                    {
                        for (int x = 0; x <= filesCheckList.CheckedItems.Count - 1; x++)
                        {
                            globalvar.fName = filesCheckList.CheckedItems[x].ToString();

                            DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);

                            //DirectoryInfo[] directories = mydir.GetDirectories(".", SearchOption.TopDirectoryOnly);

                            FileInfo[] filesx = mydir.GetFiles(".", SearchOption.AllDirectories);

                            FileInfo[] f = mydir.GetFiles();

                            foreach (FileInfo file in filesx)
                            {
                                //try
                                {
                                    if (file.Name == globalvar.fName)
                                    {
                                        if (file.Name != "History.txt")
                                            File.Move(file.FullName, file.FullName.ToString().Replace(globalvar.FIL, globalvar.REP));
                                    }
                                }

                                /*****************************
                                 globalvar.DIR = dirTxt.Text;
                DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);

                FileInfo[] filesx = mydir.GetFiles(".", SearchOption.AllDirectories);

                FileInfo[] f = mydir.GetFiles();

                filesCheckList.Items.Clear();

                foreach (FileInfo file in filesx)
                {
                    if (file.Name != "History.txt")
                        filesCheckList.Items.Add(file.Name);
                }
                                 * */
                               /* catch (IOException)
                                {
                                    MessageBox.Show("File names cannot be the same in the same folder");
                                    MessageBox.Show("File name cannot be empty");
                                }
                                catch (ArgumentException)
                                {
                                    MessageBox.Show("Cannot contain symbols / \\ : * ? < > | \" \n File name cannot be con");
                                }*/
                            }
                        }
                    }
                }

                catch (ArgumentNullException)
                {
                    MessageBox.Show("Please key in a directory");
                }

                //if (File.Exists(globalvar.DIR + @"\History.txt"))
                try
                {
                    string content = File.ReadAllText(globalvar.DIR + @"\History.txt");
                    File.WriteAllText(globalvar.DIR + @"\History.txt", String.Empty);

                   /* using (System.IO.StreamWriter files = new System.IO.StreamWriter(globalvar.DIR + @"\History.txt", true))
                    {

                        files.WriteLine("For multiple files:");
                        files.WriteLine("Changed " + "\"" + globalvar.FIL + "\"" + " to " + "\"" + globalvar.REP + "\"");
                        DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);

                        FileInfo[] f = mydir.GetFiles();
                        globalvar.clear = 1;
                        foreach (FileInfo file in f)
                        {
                            if (file.Name != "History.txt")
                            files.WriteLine(file.Name);
                        }
                        for (int x = 0; x <= filesCheckList.CheckedItems.Count - 1; x++)
                        {
                            files.WriteLine("in " + filesCheckList.CheckedItems[x].ToString() + "\n");
                        }
                        files.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
                        files.WriteLine("\n");
                        files.WriteLine(content);                        
                    } */
                }

                catch (FileNotFoundException)
                {
                    using (System.IO.StreamWriter files = File.CreateText(globalvar.DIR + @"\History.txt"))
                    {
                        files.WriteLine("For multiple files:");
                        files.WriteLine("Changed " + "\"" + globalvar.FIL + "\"" + " to " + "\"" + globalvar.REP + "\"");
                        files.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
                        files.WriteLine("\n");
                    }
                }
                MessageBox.Show("Your selected files have been changed");  
            }

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        public static class globalvar
        {
            public static string DIR;
            public static string FIL;
            public static string REP;
            public static string OPT;
            public static string fName;
            public static int clear = 0;
            public static int proceed = 0;
            public static int filpro = 0;
            public static int cignm = 0;
            public static int mngic = 0;
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                dirTxt.Text = folderBrowserDialog1.SelectedPath;
        }

        private void shfilButton_Click(object sender, EventArgs e)
        {
            applyButton.Enabled = true;

            try
            { 
                globalvar.DIR = dirTxt.Text;
                DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);

                FileInfo[] filesx = mydir.GetFiles(".", SearchOption.AllDirectories);

                FileInfo[] f = mydir.GetFiles();

                filesCheckList.Items.Clear();

                foreach (FileInfo file in filesx)
                {
                    if (file.Name != "History.txt")
                        filesCheckList.Items.Add(file.Name);
                }
            }

            catch (DirectoryNotFoundException)
            {
                MessageBox.Show("Directory not found");
            }

            catch (ArgumentException)
            {
                MessageBox.Show("Directory not found");
            }

            catch (IOException)
            {
                MessageBox.Show("Directory is invalid");
            }            
        }

        private void filChk_CheckedChanged(object sender, EventArgs e)
        {
            if (filChk.Checked)
            {
                for (int i = 0; i < filesCheckList.Items.Count; i++)
                {
                    filesCheckList.SetItemChecked(i, true);
                }
            }
            else
            {
                for (int i = 0; i < filesCheckList.Items.Count; i++)
                {
                    filesCheckList.SetItemChecked(i, false);
                }
            }                
        }

        private void dirTxt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {

                    globalvar.DIR = dirTxt.Text;
                    DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);

                    FileInfo[] f = mydir.GetFiles();
                    globalvar.clear = 1;
                    filesCheckList.Items.Clear();
                    foreach (FileInfo file in f)
                    {
                        if (file.Name != "History.txt")
                            filesCheckList.Items.Add(file.Name);
                    }
                }

                catch (DirectoryNotFoundException)
                {
                    MessageBox.Show("Directory not found");
                }

                catch (ArgumentException)
                {
                    MessageBox.Show("Directory not found");
                }

                catch (IOException)
                {
                    MessageBox.Show("Directory is invalid");
                }
            }
        }

        private void dirTxt_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

```