How to move file from one directory to another in C#

3 Answers

0 votes
using System;
using System.IO;

class Program
{
    static void Main() {
        string file = @"d:\data.txt";
 
        try {
            File.Move(file, @"d:\development - tools\data.txt"); 
 
            Console.WriteLine("Move successes");
             
        }
        catch (IOException ioex) {
            Console.WriteLine(ioex.Message);
        }
    }
}




/*
run:

Cannot create a file when that file already exists.

*/


answered Mar 17, 2015 by avibootz
edited Sep 2, 2023 by avibootz
0 votes
using System;
using System.IO;

class Program
{
    static void Main() {
         string file = @"d:\somefile.txt";
 
        try {
            File.Move(file, @"d:\development - tools\data.txt"); 
 
            Console.WriteLine("Move successes");
        }
            
        catch (IOException ioex) {
            Console.WriteLine(ioex.Message);
        }
    }
}




/*
run:

Could not find file 'd:\somefile.txt'.

*/


answered Mar 17, 2015 by avibootz
edited Sep 2, 2023 by avibootz
0 votes
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            string source = @"D:\date.bin";
            string target = @"D:\temp\date.bin";
 
            File.Move(source, target);
        }
    }
}
 
 
 
 
/*
 * run:
 *
 *
 * 
 */

 



answered Sep 2, 2023 by avibootz

Related questions

1 answer 241 views
1 answer 191 views
1 answer 221 views
2 answers 228 views
3 answers 282 views
1 answer 263 views
3 answers 323 views
...