Category: C# Questions

How to Get Application Path in C#/CSharp

To get application path in C#/CSharp, we make use of GetExecutingAssembly() static method of Assembly class, which resides in System.Reflection namespace.  Example is given below. using System; using System.Reflection; namespace Hello_World { class Program { static void Main(string args) { string path = Assembly.GetExecutingAssembly().Location; Console.WriteLine(path); } } }

How to Copy File in C#/CSharp

To copy a file in C#/CSharp, we make use of File class and its method Copy.  This class resides in System.IO namespace.  Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { //Source of file string source = @"C:\Users\ADMIN\Desktop\folder1\file.txt"; //Destination Folder string destination = @"C:\Users\ADMIN\Desktop\folder2\"; //Destination

How to Delete a File in C#/CSharp

To delete a file in C#/CSharp, we make use of File class and its static method called Delete.  It will take path of the file as parameter.  Example is given below. using System; using System.IO; namespace Hello_World { class Program { static void Main(string args) { string path = @"C:\Users\ADMIN\Desktop\delete.txt"; File.Delete(path); Console.WriteLine("File Deleted!"); } }

How to Get File Size in C#/CSharp

To get file size in C#/CSharp, we make use FileInfo 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) { long filesize = new FileInfo("filname.txt").Length; Console.WriteLine(filesize); } } }