Found 34494 Articles for Programming

C# Program to get the last access time of a file

Samual Sam
Updated on 22-Jun-2020 14:43:40

673 Views

To get the last access time of a file in C#, use the LastAccessTime() method.For this, use the FileInfo as well as DateTime classes.Create an object of each −FileInfo file = new FileInfo("new.txt"); DateTime dt = file.CreationTime; dt = file.LastAccessTime;Let us see the complete code −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("quiz.txt")) {          sw.WriteLine("Quizzes!");       }       FileInfo file = new FileInfo("quiz.txt");       // last access time       DateTime dt = file.LastAccessTime;       Console.WriteLine(dt);    } }Output9/5/2018 5:21:43 AM

C# Program to get the last write time of a file

karthikeya Boyini
Updated on 22-Jun-2020 14:44:14

2K+ Views

To get the last write time of a file in C#, use the LastWriteTime() method.For this, use the FileInfo as well as DateTime classes.Create an object of each −FileInfo file = new FileInfo("amit.txt"); DateTime dt = file.CreationTime; dt = file.LastWriteTime;Let us see the complete code −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("amit.txt")) {          sw.WriteLine("Welcome!");       }       FileInfo file = new FileInfo("amit.txt");       // file creation time       DateTime dt = ... Read More

C# Program to get information about a file

Samual Sam
Updated on 22-Jun-2020 14:34:31

136 Views

To get information about a file means to get what all attributes are set for that particular file. For example, a file can be normal, hidden, archived, etc.Firstly, use the FileInfo class −FileInfo info = new FileInfo("hello.txt");Now, use FileAttributes to get information about a file −FileAttributes attr = info.Attributes;The following is the code −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("hello.txt")) {          sw.WriteLine("This is demo text!");       }       FileInfo info = new FileInfo("hello.txt");       FileAttributes attr = info.Attributes;       Console.WriteLine(attr);    } }OutputNormal

C# Program to create new directories

karthikeya Boyini
Updated on 22-Jun-2020 14:34:54

107 Views

Use the CreateDirectory method to create a directory.Let’s say you need to create a directory in D drive. For that, use the CreateDirectory() method as shown below −Directory.CreateDirectory("D:\Tutorial");The following is the code −Exampleusing System.IO; using System; public class Program {    public static void Main() {       Directory.CreateDirectory("D:\Tutorial");    } }

C# Program to display the name of the directory

Samual Sam
Updated on 22-Jun-2020 14:35:19

96 Views

Firstly, use the DirectoryInfo that operates on Directories. The parameter set in it is the file path −DirectoryInfo dir = new DirectoryInfo(@"D:ew\");To get the name of the directory, use the Name property −dir.NameThe following is the code to display the name of the directory −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       DirectoryInfo dir = new DirectoryInfo(@"D:ew\");       // displaying the name of the directory       Console.WriteLine(dir.Name);    } }OutputD:ew\

C# Program to check whether a directory exists or not

karthikeya Boyini
Updated on 22-Jun-2020 14:35:49

186 Views

Use the Directory. Exists method to check whether a directory exists or not.Let’s say you need to check whether the following directory exists or not −C:\AmitFor that, use the Exists() method −if (Directory.Exists("C:\Amit")) {    Console.WriteLine("Directory Amit Exist!"); }The following is the complete code −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       if (Directory.Exists("C:\Amit")) {          Console.WriteLine("Directory Amit Exist!");       } else {       Console.WriteLine("Directory Amit does not exist!");       }    } }OutputDirectory Amit does not exist!

C# Program to write an array to a file

Samual Sam
Updated on 22-Jun-2020 14:36:30

4K+ Views

Use WriteAllLines method to write an array to a file.Firstly, set a string array −string[] stringArray = new string[] {    "one",    "two",    "three" };Now, use the WriteAllLines method to add the above array to a file −File.WriteAllLines("new.txt", stringArray);Here is the complete code −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       string[] stringArray = new string[] {          "one",          "two",          "three"       };       File.WriteAllLines("new.txt", stringArray);       using (StreamReader sr = new StreamReader("new.txt")) {          string res = sr.ReadToEnd();          Console.WriteLine(res);       }    } }Outputone two three

C# Program to count the number of lines in a file

karthikeya Boyini
Updated on 22-Jun-2020 14:37:06

2K+ Views

Firstly, create a file using StreamWriter class and add content to it −using (StreamWriter sw = new StreamWriter("hello.txt")) {    sw.WriteLine("This is demo line 1");    sw.WriteLine("This is demo line 2");    sw.WriteLine("This is demo line 3"); }Now use the ReadAllLines() method to read all the lines. With that, the Length property is to be used to get the count of lines −int count = File.ReadAllLines("hello.txt").Length;Here is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.IO; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("hello.txt")) {       ... Read More

Case-insensitive Dictionary in C#

Samual Sam
Updated on 22-Jun-2020 14:37:38

2K+ Views

To compare, ignoring case, use the case-insensitive Dictionary.While declaring a Dictionary, set the following property to get case-insensitive Dictionary −StringComparer.OrdinalIgnoreCaseAdd the property like this −Dictionary dict = new Dictionary (StringComparer.OrdinalIgnoreCase);Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       Dictionary dict = new Dictionary       (StringComparer.OrdinalIgnoreCase);       dict.Add("cricket", 1);       dict.Add("football", 2);       foreach (var val in dict) {          Console.WriteLine(val.ToString());       }       // case insensitive dictionary i.e. "cricket" is equal to "CRICKET"       Console.WriteLine(dict["cricket"]);       Console.WriteLine(dict["CRICKET"]);    } }Output[cricket, 1] [football, 2] 1 1

C# Program to combine Dictionary of two keys

karthikeya Boyini
Updated on 22-Jun-2020 14:38:25

659 Views

Firstly, set the Dictionaries to be combined −Dictionary dict1 = new Dictionary (); dict1.Add("one", 1); dict1.Add("Two", 2); Dictionary dict2 = new Dictionary (); dict2.Add("Three", 3); dict2.Add("Four", 4);Now, use HashSet to combine them. The method used for the same purpose is UnionWith() −HashSet hSet = new HashSet (dict1.Keys); hSet.UnionWith(dict2.Keys);The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       Dictionary dict1 = new Dictionary ();       dict1.Add("one", 1);       dict1.Add("Two", 2);       Dictionary ... Read More

Advertisements