CSharp - Write program to appends .png extension to file name unless the extension is .png


You will write a program that appends the .png extension to the entered file name unless the extension is already part of the input.


First, you convert the file name to lowercase so you do not have to distinguish between .png and .PNG.

You use the method EndsWith to find whether the text ends or does not end with something specific.


using System;
class Program//  w w  w  .  j  ava 2s .c o  m
    static void Main(string[] args)
        // Input 
        Console.Write("Enter image name: ");
        string fileName = Console.ReadLine();

        // Appending extension (ONLY IN CASE OF NEED) 
        if (!fileName.ToLower().EndsWith(".png"))
            fileName += ".png";

        // Output 
        Console.WriteLine("We are going to use name: " + fileName);