Monthly Archives: June 2020

Special paths

Executing path

I sometimes want to get the path of the .exe file that is currently running, usually because I put some folders for logging or configuration there, this is a way to get this path:

// get base directory, entry point
string entryPoint = Assembly.GetEntryAssembly().Location;
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

// create directory name
string configDirectory = Path.Combine(baseDirectory, "config");

Console.WriteLine($"entryPoint:      {entryPoint}");
Console.WriteLine($"baseDirectory:   {baseDirectory}");
Console.WriteLine($"configDirectory: {configDirectory}");
entryPoint:      C:\Users\yourUserName\ConsoleApp\bin\Debug\ConsoleApp.exe
baseDirectory:   C:\Users\yourUserName\ConsoleApp\bin\Debug\
configDirectory: C:\Users\yourUserName\ConsoleApp\bin\Debug\config

https://stackoverflow.com/questions/3991933/get-path-for-my-exe

Special folders

Here is a list of all C# Environment.SpecialFolder:

var allSpecialFolders = Enum.GetValues(typeof(Environment.SpecialFolder)).Cast<Environment.SpecialFolder>();

foreach (Environment.SpecialFolder specialFolder in allSpecialFolders)
{
    Console.WriteLine($"{specialFolder.ToString().PadRight(22)} = {Environment.GetFolderPath(specialFolder)}");
}
Desktop                = C:\Users\yourUserName\Desktop
Programs               = C:\Users\yourUserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
MyDocuments            = C:\Users\yourUserName\Documents
MyDocuments            = C:\Users\yourUserName\Documents
Favorites              = C:\Users\yourUserName\Favorites
Startup                = C:\Users\yourUserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Recent                 = C:\Users\yourUserName\AppData\Roaming\Microsoft\Windows\Recent
SendTo                 = C:\Users\yourUserName\AppData\Roaming\Microsoft\Windows\SendTo
StartMenu              = C:\Users\yourUserName\AppData\Roaming\Microsoft\Windows\Start Menu
MyMusic                = C:\Users\yourUserName\Music
MyVideos               = C:\Users\yourUserName\Videos
DesktopDirectory       = C:\Users\yourUserName\Desktop
MyComputer             =
NetworkShortcuts       = C:\Users\yourUserName\AppData\Roaming\Microsoft\Windows\Network Shortcuts
Fonts                  = C:\WINDOWS\Fonts
Templates              = C:\Users\yourUserName\AppData\Roaming\Microsoft\Windows\Templates
CommonStartMenu        = C:\ProgramData\Microsoft\Windows\Start Menu
CommonPrograms         = C:\ProgramData\Microsoft\Windows\Start Menu\Programs
CommonStartup          = C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
CommonDesktopDirectory = C:\Users\Public\Desktop
ApplicationData        = C:\Users\yourUserName\AppData\Roaming
PrinterShortcuts       =
LocalApplicationData   = C:\Users\yourUserName\AppData\Local
InternetCache          = C:\Users\yourUserName\AppData\Local\Microsoft\Windows\INetCache
Cookies                = C:\Users\yourUserName\AppData\Local\Microsoft\Windows\INetCookies
History                = C:\Users\yourUserName\AppData\Local\Microsoft\Windows\History
CommonApplicationData  = C:\ProgramData
Windows                = C:\WINDOWS
System                 = C:\WINDOWS\system32
ProgramFiles           = C:\Program Files
MyPictures             = C:\Users\yourUserName\Pictures
UserProfile            = C:\Users\yourUserName
SystemX86              = C:\WINDOWS\SysWOW64
ProgramFilesX86        = C:\Program Files (x86)
CommonProgramFiles     = C:\Program Files\Common Files
CommonProgramFilesX86  = C:\Program Files (x86)\Common Files
CommonTemplates        = C:\ProgramData\Microsoft\Windows\Templates
CommonDocuments        = C:\Users\Public\Documents
CommonAdminTools       = C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools
AdminTools             = C:\Users\yourUserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools
CommonMusic            = C:\Users\Public\Music
CommonPictures         = C:\Users\Public\Pictures
CommonVideos           = C:\Users\Public\Videos
Resources              = C:\WINDOWS\resources
LocalizedResources     =
CommonOemLinks         =
CDBurning              = C:\Users\yourUserName\AppData\Local\Microsoft\Windows\Burn\Burn

Advertisement

Regex in C#

Remove empty spaces from string

string sentence = " this is      a sentence with many     empty spaces      ";
sentence = sentence.Trim();
sentence = Regex.Replace(sentence, @"\s+", " "):

Regex matching

Regex regex = new Regex(@"@@\S+");
Match match = regex.Match("this is a @@specialword in my string");

if (match.Success)
{
  Console.WriteLine("Regex found: " + match.Value);
}

Test your Regex

A good site to get an overview of what Regex has to offer and directly test it is https://regex101.com/

DateTime

Constructor

DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 0);
Console.WriteLine(date1.ToString());
// The example displays the following output, in this case for en-us culture:
//      8/18/2010 4:32:00 PM

https://docs.microsoft.com/en-us/dotnet/api/system.datetime.-ctor?view=netcore-3.1

ToString() method

In this example I use DateTime.Now.ToString(“yyyyMMdd_HHmmss”) in order to create a dated file Name that will look like this:

20200610091524_myText.txt

string folder = Environment.SpecialFolder.MyDocuments;
string currentDate = DateTime.Now.ToString("yyyyMMdd_HHmmss");
string fileName = Path.Combine(folder, $"{currentDate}_myText.txt");

https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings