How do I save some files under a directory in a new directory after making some changes

+1 Mohamed witoo · November 9, 2014
I opened a directory and I read some files in that directory amd made some changes, now I want to save the changes with the same file names at F:\BI\Out\ and keep the original files.
Can you help me Thanks

        string text = "";
        string[] files;

        private void Form1_Load(object sender, EventArgs e)
        {

            try
            {
                files = Directory.GetFiles(@"F:\BI\In\", "*.*", SearchOption.AllDirectories);
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtEtlPath.Text == "")
            {
                MessageBox.Show("Please Enter path");
                txtEtlPath.Focus();
            }

            else
            {

                foreach (string file in files)
                {
                    if (System.IO.File.Exists(file))
                    {
                        text = File.ReadAllText(file);
                        text = text.Replace("Company_Address", txtCompanyAddress.Text + "_BI");
                        text = text.Replace("Company_Name", txtCompanyName.Text.Trim() + "_BIDW");
                        text = text.Replace("C:\\BIfolder",cboDrive.Text + txtEtlPath.Text.Trim());

                    }

Post a Reply

Replies

Oldest  Newest  Rating
0 Eugene Botma · November 9, 2014
you need to write the string to the new file in the new directory.

here are the basic commands for creating a directory and writing text to a file. You should be able to figure out how to complete your program with this.

string text = "your text content";
Directory.CreateDirectory("directory name");
File.WriteAllText("directory name/file name.txt", text);
0 Mohamed witoo · November 10, 2014
Thanks for help
  • 1

C#

107,054 followers
About

C# is an object-oriented language designed for improving the development of web applications.

Links
Moderators