How to copy a file to another location in C#

4 Answers

0 votes
using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            File.Copy("d:\\data.txt", "d:\\development - tools\\data.txt");

            Console.WriteLine(File.ReadAllText("d:\\development - tools\\data.txt"));
        }
    }
}

/*
run:
   
d:\\development - tools\\data.txt
---------------------------------
a b c

*/


answered Mar 15, 2015 by avibootz
0 votes
using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            if (File.Exists("d:\\development - tools\\data.txt") == false)
                File.Copy("d:\\data.txt", "d:\\development - tools\\data.txt");

            Console.WriteLine(File.ReadAllText("d:\\development - tools\\data.txt"));
        }
    }
}

/*
run:
   
d:\\development - tools\\data.txt
---------------------------------
a b c

*/


answered Mar 15, 2015 by avibootz
0 votes
using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                File.Copy("d:\\datadatadata.txt", "d:\\development - tools\\data.txt", true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:
   
Could not find file 'd:\datadatadata.txt'.

*/


answered Mar 15, 2015 by avibootz
0 votes
using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                File.Copy("d:\\data.txt", "d:\\development - tools\\data.txt");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:
   
The file 'd:\development - tools\data.txt' already exists.

*/


answered Mar 15, 2015 by avibootz

Related questions

1 answer 209 views
1 answer 230 views
1 answer 154 views
154 views asked Aug 15, 2022 by avibootz
1 answer 120 views
2 answers 227 views
227 views asked May 5, 2020 by avibootz
...