Wednesday, January 25, 2012

Wednesday 1-25-12

class Program
{
static void Main(string[] args)
{
ShowFolders();
Console.WriteLine();
ShowFolders(@"C:\");
}

static void ShowFolders(string root = @"C:\", bool showFullPath = false)
{
foreach (string folder in Directory.EnumerateDirectories(root))
{
string output = showFullPath ? folder : Path.GetFileName(folder);
Console.WriteLine(output);
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace fromTheWeb
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("My machine name: {0}", Environment.MachineName);
Console.WriteLine();

//get drives availabe on local computer and form into a single character expression
string[] drives = Environment.GetLogicalDrives();
string driveNames = string.Empty;

foreach (string drive in drives)
driveNames += drive.Substring(0, 1);

//create regular expression pattern dynamically based on local machine information
string pattern = @"\\\\(?i:" + Environment.MachineName + @")(?:\.\w+)*\\((?i:[" + driveNames + @"]))\$";

string replacement = "$1:";

string[] uncPaths = { @"\\" + Environment.MachineName + @".domain1.mycompany.com\C$\Thingstodo.txt",
@"\\" + Environment.MachineName + @"\c$\ThingsToDo.txt",
@"\\" + Environment.MachineName + @"\d$\documents\mydocument.docx"};

foreach (string uncPath in uncPaths)
{
Console.WriteLine("Input String: " + uncPath);
Console.WriteLine("Returned String: " + Regex.Replace(uncPath, pattern, replacement));
Console.WriteLine();
}
}
}
}

No comments:

Post a Comment