Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Server Side Programming Articles - Page 2452 of 2650
435 Views
EnumerateFile() method is used in C# to get all the files. Use AllDirectories property to recurse through directories −Directory.EnumerateFiles(@"D:\NEW", "*.*", SearchOption.AllDirectories)To get the list of files in a directory, use the SearchOptions.AllDirectories in C# as shown above.Let us see how −Exampleusing System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { foreach (string allFiles is Directory.EnumerateFiles(@"D:\NEW","*.*",SearchOption.AllDirectories)) { Console.WriteLine(allFiles); } } } }OutputThe following is the output −D:\NEW\my.txt D:\NEW\amit.html D:\NEW\tutorials\java\a.java
241 Views
To get all the disk drives on a system, use the GetLogicalDrives() method in C# −Environment.GetLogicalDrives()Use it with Join method to get the comma-separated list of logical drives −string.Join(",", Environment.GetLogicalDrives())Example Live Demousing System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { Console.WriteLine(string.Join(",", Environment.GetLogicalDrives())); } } }Output/,/etc/resolv.conf,/etc/hostname,/etc/hosts,/run/secrets,/home/cg/root
2K+ Views
The Enviornment.NewLine in C# is used to add newline.To set a new line in between words −str = "This is demo text!" + Environment.NewLine + "This is demo text on next line!";The following is the code −Example Live Demousing System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { string str = "This is demo text!" + Environment.NewLine + "This is demo text on next line!"; Console.Write(str); } } }OutputThis is demo text! This is demo text on next line!
662 Views
Set the path name in a string −string myPath = "D:ew\quiz.txt";Now, use the GetFileName() method to get the name of the file −Path.GetFileName(myPath)The following is the complete code −Example Live Demousing System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { string myPath = "D:ew\quiz.txt"; // get extension Console.WriteLine("Extension: "+Path.GetExtension(myPath)); // get path Console.WriteLine("File Path: "+Path.GetFileName(myPath)); } } }OutputExtension: .txt File Path: D:ew\quiz.txt
307 Views
To handle file paths, the Path class is used in C#.Set the file name in a string −string myPath = "D:ew\quiz.txt";Now, to get the extension, use the GetExtension() method −Path.GetExtension(myPath)Here is the complete code −Example Live Demousing System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { string myPath = "D:ew\quiz.txt"; Console.WriteLine(Path.GetExtension(myPath)); } } }Output.txt
215 Views
Use the Environment.ProcessorCount to get the total number of cores on a computer −Environment.ProcessorCountThe following is the code that displays the total number of cores on a computer in C# −ExampleUsing System; namespace Demo { class Program { static void Main(string[] args) { Console.WriteLine(Environment.ProcessorCount); } } }
2K+ Views
Set a string −string str = "Bit and Bat";Let’s say you need to replace whatever comes inside B and t to A and capitalize the complete string. For that, use Replace −Regex.Replace(str, "B.t", "BAT");Let us see the complete code −Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo { class Program { static void Main(string[] args) { string str = "Bit and Bat"; Console.WriteLine(str); string res = Regex.Replace(str, "B.t", "BAT"); Console.WriteLine(res); } } }OutputBit and Bat BAT and BAT
564 Views
Use the Regex.Replace method to remove the end part of a string in C#.The following is the string −string s1 = "Demo Text!";Now, let us say you need to remove the exclamation mark (!) from the string. For that just set it to empty using replace −System.Text.RegularExpressions.Regex.Replace(s1, "!", "");Here is the complete code −Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo { class Program { static void Main(string[] args) { string s1 = "Demo Text!"; // replace the end part string s2 = System.Text.RegularExpressions.Regex.Replace(s1, "!", ... Read More
392 Views
To match all digits in a string, use C# Regex.Firstly, set a string with digits −string str = "These are my marks: 90 out of 100!";Use the following regular expression to get digits in a string −@"\d+"The following is the code −Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo { class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); ... Read More
2K+ Views
Firstly, set Random class −Random random = new Random();Set a range under the Next() method. This displays a letter between 0 and 26.int a = random.Next(0, 26);Here is the complete code −Example Live Demousing System; using System.IO; using System.Linq; class Demo { static void Main() { Random random = new Random(); // random lowercase letter int a = random.Next(0, 26); char ch = (char)('a' + a); Console.WriteLine(ch); } }Outputt