Quantcast
Channel: User BobbyShaftoe - Stack Overflow
Viewing all articles
Browse latest Browse all 45

Answer by BobbyShaftoe for How do I get/set a winforms application's working directory?

$
0
0

So, you can change directory by just using Environment.CurrentDirectory = (sum directory). There are many ways to get the original executing directory, one way is essentially the way you described and another is through Directory.GetCurrentDirectory() if you have not changed the directory.

using System;using System.IO;class Test {    public static void Main()     {        try         {            // Get the current directory.            string path = Directory.GetCurrentDirectory();            string target = @"c:\temp";            Console.WriteLine("The current directory is {0}", path);            if (!Directory.Exists(target))             {                Directory.CreateDirectory(target);            }            // Change the current directory.            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


Viewing all articles
Browse latest Browse all 45

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>