C# vs Python — Examples
Move A File
using System;
using System.IO;
class Program
{
static void Main()
{
// Define source and destination paths
string sourcePath = @"C:\path\to\source\file.txt";
string destinationPath = @"C:\path\to\destination\file.txt";
try
{
// Make sure destination directory exists
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
// Move the file
File.Move(sourcePath, destinationPath);
Console.WriteLine($"File moved from {sourcePath} to {destinationPath}");
}
catch (Exception ex)
{
Console.WriteLine($"Error moving file: {ex.Message}");
}
}
}
import shutil #shell utilities (for moving and copying files)
import OS #operating system
# Define source and destination paths
source_path = "C:/path/to/source/file.txt"
destination_path = "C:/path/to/destination/file.txt"
# Make sure destination directory exists
OS.makedirs(OS.path.dirname(destination_path), exist_ok=True)
# Move the file
shutil.move(source_path, destination_path)
print(f"File moved from {source_path} to {destination_path}")