So you can change the directory simply by using Envrionment.CurrentDirectory = (sum directory). There are many ways to get the original directoy executable, one of which is essentially described by you, and the other through Directory.GetCurrentDirectory (), if you have not changed the directory.
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
string path = Directory.GetCurrentDirectory();
string target = @"c:\temp";
Console.WriteLine("The current directory is {0}", path);
if (!Directory.Exists(target))
{
Directory.CreateDirectory(target);
}
Environment.CurrentDirectory = (target);
if (path.Equals(Directory.GetCurrentDirectory()))
{
Console.WriteLine("You are in the temp directory.");
}
else
{
Console.WriteLine("You are not in the temp directory.");
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
ref
source
share