Category: C# Questions

How to Replace a String in C#/CSharp

To replace a string in C#/CSharp, we make use Replace method.  Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { string hello = "Hello World"; string myString = hello.Replace("World", "FWait"); Console.WriteLine(myString); } } }

How to Reverse a String in C#/CSharp

To reverse a string in C#/CSharp, we first convert string to an array of characters.  Then, use static method Reverse which resides in Array class to reverse the string.  Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { string hello = "Hello World"; char array

How to Check If File Exists in C#/CSharp

To check if file exists in C#/CSharp, we make use of static function Exists of File class.  This will return a boolean value.  Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { string path = @"C:\file.txt"; if(File.Exists(path)) { Console.WriteLine("File Exists: Yes!"); } else { Console.WriteLine("File

How to Get File Name from Path in C#/CSharp

To get file name from path in C#/CSharp, we make use of GetFileName() static method of Path class, which resides in System.IO namespace.  Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { string file = @"C:\Users\ADMIN\Desktop\delete.txt"; string name = Path.GetFileName(file); Console.WriteLine(name); } } }

How to Get File Extension in C#/CSharp

To get file extension in C#, we make use of GetExtension() static method of Path class, which resides in System.IO namespace.  Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { string file = @"C:\Users\ADMIN\Desktop\delete.txt"; string extension = Path.GetExtension(file); Console.WriteLine(extension); } } }