---
title: "C# Change current FileInfo file?"  
description: "C# Change current FileInfo file?"  
author: "Royce Roy"  
published: 2013-12-18  
updated: 2013-12-18  
canonical: https://www.mindstick.com/forum/1813/c-sharp-change-current-fileinfo-file  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# C# Change current FileInfo file?

I'd like to change the [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) that the FileInfo object is currently using. Let [say](https://answers.mindstick.com/qa/116645/a1-wreckers-reviews-what-do-customers-say) I want to loop through 1000 files.

```
FileInfo myFile = new FileInfo("myfile.txt");myFile.ChangeFile("myfile2.txt");                   
```

How can I do this? Was hoping for .FileName =, but it's readonly.

## Replies

### Reply by Anonymous User

Hi Royce,

You can't do this in c# you don't have a built-in function use this function instead

```
 private void ChangeFiles(string fPath, string fNewName){    string fExt;    string fFromName;    string fToName;    int i = 1;    //copy all files from fPath to files array    FileInfo[] files = new DirectoryInfo(fPath).GetFiles();    //loop through all files    foreach (var f in files)    {        //get the filename without the extension        fFromName = Path.GetFileNameWithoutExtension(f.Name);        //get the file extension        fExt = Path.GetExtension(f.Name);        //set fFromName to the path + name of the existing file        fFromName = string.Format("{0}{1}", fPath, f.Name);        //set the fToName as path + new name + _i + file extension        fToName = string.Format("{0}{1}_{2}{3}", fPath, fNewName,i.ToString(), fExt);        //rename the file by moving to the same place and renaming        File.Move(fFromName, fToName);        //increment i        i++;    }}
```


---

Original Source: https://www.mindstick.com/forum/1813/c-sharp-change-current-fileinfo-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
