×

C# Tutorial

Basics of .Net

C# Basics

C# Fundamentals

C# Operators

C# Loops

C# Type Conversions

C# Exception Handling

C# String Class

C# Arrays

C# List

C# Stack

C# Queue

C# Collections

C# Character Class

C# Class and Object

C# Namespaces

C# Delegates

C# Constructors

C# Inheritance

C# Operator Overloading

C# Structures

C# File Handling

C# Convert.ToInt32()

C# Int32 (int) Struct

C# DateTime Class

C# Uri Class

C# Database Connectivity

C# Windows

C# Other Topics

C# Q & A

C# Programs

C# Find O/P

C# - How to Move a File or Directory?

Learn, how to move a file from one location to another location uisng C# program?
Submitted by IncludeHelp, on November 03, 2017 [Last updated : March 26, 2023]

Given a file, and we have to move it from one location to another location using C# program.

To move a file or directory in C#, we use File.Move() method.

File.Move()

This is a method of "File" class, which is used to move a file from one location (source) to another location (destination).

Syntax

File.Move(source_file, dest_file);

Parameter(s)

  1. source_file - Location of source file.
  2. dest_file - Location of destination file, and here we can change filename.

We can set following detail of last write time:

  • Date
  • Month
  • Year
  • Hour
  • Minute
  • Second
  • AM/PM

C# program to move a file or directory

using System;
using System.IO;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      File.Move("source/ABC.TXT", "dest/XYZ.TXT");
      Console.WriteLine("File moved successfully");
    }
  }
}

Output

File moved successfully

Explanation

In the above program, we need to remember, when we use "File" class, System.IO namespace must be included in the program.

C# File Handling Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.