- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34494 Articles for Programming
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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"); } }
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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\
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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!
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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