Found 34486 Articles for Programming

Literal number suffixes in C#

karthikeya Boyini
Updated on 22-Jun-2020 14:42:15

274 Views

To specify number types, use suffixes in C#. Literal number suffixes are numeric suffixes.For example, for long type −// long suffix long val1 = 29345L;For double type −// double suffix double val2 = 297.325D; Console.WriteLine(val2);Let us see more examples −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       // long suffix       long val1 = 29345L;       Console.WriteLine(val1);       // decimal suffix       decimal val5 = 3245.5678M;       Console.WriteLine(val5);       // double suffix       double val2 = 297.325D;       Console.WriteLine(val2);       // float suffix       float val3 = 250.35F;       Console.WriteLine(val3);       // unsigned suffix       uint val4 = 3456U;       Console.WriteLine(val4);    } }Output29345 3245.5678 297.325 250.35 3456

Lowercase suffixes in C#

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

101 Views

Set lowercase suffixes for literals such as u, l, ul, f, etc.// l for long long a = 29876l;It can be used on literal numbers as well. It tells the compiler that the literal is of a specific type.The following is an example −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       long a = 29876l;       float b = 95.10f;       Console.WriteLine(a);       Console.WriteLine(b);    } }On running the above example, you will get the following output. With that, you will also get a ... Read More

Get the creation time of a file in C#

karthikeya Boyini
Updated on 22-Jun-2020 14:43:02

2K+ Views

To get the creation time of a file in C#, use the CreationTime() 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;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("qa.txt")) {          sw.WriteLine("Questions and Answers!");       }       FileInfo file = new FileInfo("qa.txt");       // file creation time       DateTime dt = file.CreationTime;       Console.WriteLine(dt);    } }Output9/5/2018 5:20:03 AM

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

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

675 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

137 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

108 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

Advertisements