TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Path.ChangeExtension

This C# article shows how to use the Path.ChangeExtension method. It removes an extension with null.

Path.ChangeExtension modifies a path string.

It can, for example, change a file ending in a ".txt" extension to end in the "" extension. This makes it unnecessary to write custom string methods.

Note: Use ChangeExtension with string arguments such as ".txt". You can use null to remove the entire extension.

Example. First, we use the Path.ChangeExtension static method from the System.IO namespace to change the extension of a path string in the program's memory. The Path class does not actually access the disk or persist the change.

Static Method

Instead, it changes the path string's memory representation and returns a reference to the new string data. After you receive the results of Path.ChangeExtension, you must write the new file to disk or move the old one.

C# program that changes path extension

using System.IO;

class Program
{
    static void Main()
    {
	// The file name.
	string path = "test.txt";

	// Make sure the file exists.
	File.WriteAllText(path, "Tutorial file");

	// Change the path name to have a new extension in memory.
	string changed = Path.ChangeExtension(path, "");

	// Create file with the new extension name.
	File.WriteAllText(changed, "Changed file");
    }
}

Result
    Output directory will contain two files.

test.txt
test

The program invokes File.WriteAllText to write to the disk based on the path. The Path.ChangeExtension method usage shows how to change the extension of a path to have an "" extension instead of a ".txt" extension.

Info: You must specify the leading period, not just three letters, on the extension string.

Remove extension. Next, you can remove the extension entirely by providing the null literal as the second argument to ChangeExtension. Because the Path class has no RemoveExtension method, providing null is the clearest way to remove the extension.

Tip: If you pass an empty string "" to ChangeExtension, the period is not removed.

C# program that removes extensions

using System;
using System.IO;

class Program
{
    static void Main()
    {
	string original = "file.txt";
	Console.WriteLine(Path.ChangeExtension(original, ""));
	Console.WriteLine(Path.ChangeExtension(original, null));
    }
}

Output

file.
file

Note: Thanks to Pierre-Luc Gélinas for writing in with a bug report on this section.

Summary. We used the Path.ChangeExtension method in a C# program. This method receives two parameters, the first being the path string you want to change, and the second being the new extension including the leading period.

Review: The method will throw various exceptions on invalid inputs. You may want to protect your program against these conditions.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf