How to rename(change file name) in C# ?

CK CK 3788 
Created at
Updated at  
1,080 0 0

Simplest way to change file name in C# is using File.Move() function.

Here's the simple example change file name for Samsung Black Jack Phone. However it may work on another platform if it supports .NET compact framework.

In addition, File creation time can be obtained by FileInfo class. See the below example for addition example.
 

//
// Programmed 2008 by Kurapa Chunun Kang (kurapa@kurapa.com)
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; // added for FileInfo class

namespace fnc
{
    class Program
    {
       // developed for Samsung Black Jack Smart Phone
        static void KChangeFileNameForBlackJack(string strDir)
        {
            int iocnt = 100;
            DirectoryInfo dir = new DirectoryInfo( strDir);
            FileInfo[] bmpfiles = dir.GetFiles("PIC-*.jpg");
            Console.WriteLine("Total number of bmp files", bmpfiles.Length);
            foreach (FileInfo f in bmpfiles)
            {
                String fn = f.CreationTime.Year + "-"
                            + ((f.CreationTime.Month<10) ? "0":"") + f.CreationTime.Month + "-"
                            + ((f.CreationTime.Day < 10) ? "0" : "") + f.CreationTime.Day + "_"
                            + ((f.CreationTime.Hour < 10) ? "0" : "") + f.CreationTime.Hour
                            + ((f.CreationTime.Minute < 10) ? "0" : "") + f.CreationTime.Minute
                            + ((f.CreationTime.Second < 10) ? "0" : "") + f.CreationTime.Second
                            + (iocnt++)
                            + ".jpg";
                File.Move( strDir + "" + f.Name, strDir + "" + fn);
            }
        }

        static void Main(string[] args)
        {
            KChangeFileNameForBlackJack(@"Storage CardMy Pictures");
        }
    }
}


1113679021.zip
 

Tags C# FileInfo Filename Change Rename Facebook X
Comments 0
  1. How to convert integer to text string ?
    699
  2. How to convert text string to integer ?
    611
  3. How to set an certain color as transparent color on bitmap
    517
  4. How to resize and save PNG or Jpeg Image file ?
    632
  5. How to display message box in C#
    1,112
  6. How to convert bitmap image to jpeg image ?
    356
  7. To load image from file
    451
  8. URL Encode, Decode function for MFC
    605
  9. How to load HTML resource on MFC ?
    842
  10. How to get GM Time as String ?
    423
  11. Makefile to automate the build process for C++
    280
  12. Finding process name on OS
    408
  13. Convert System.DateTime to UNIX timestamp
    381
  14. Retrieve system information
    495
  15. How to call SetTimer function on MFC CDialog class ?
    651
  16. Find Files in certain directory
    927
  17. Implementing C#'s foreach loop in Delphi 8
    460
  18. How to detect resource leaks in a Form
    940
  19. The best way to enable and disable buttons, menu items and toolbar buttons
    667
  20. How to exit from a console application
    811
  21. How to restrict a program to a single instance
    729
  22. Compress and decompress strings in C#
    972
  23. Graphics in C# - Irregular Forms
    910
  24. Extracting the Country from IP Address
    469
  25. How to run shell command by MFC ?
    439
  26. Binary Search Sample Code
    406
  27. Double Linked List based in C++
    796
  28. String Replace Function For C#
    1,548
  29. How to print character in C# by ASCII code
    477
  30. MFC based World Wide Web HTTP Server Source Code
    459
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,000
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    251
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    515
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    231
  16. TPU & GPU Cluster Management Tools
    245
  17. The Concept of Scaffolding in AI Agents
    234
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    222
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    221
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  30. Understanding Data Lineage
    262