0

So I'm learning how to develop software and I'm running into a problem. When I create a form in Visual Studio and have it open a document or open something else when I click a button I have it pointing here:

C:\User\MyName\Documents\TestApp\test.txt

What I want to know is how do I get it to where the program just looks at TestApp folder vs going through the C: Drive? Say all the files are needed for the program to run are located in the TestApp folder.

Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
Daedric
  • 15
  • 1
  • 4
  • Are you looking for http://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path ? – Corak Apr 01 '15 at 06:18
  • In relation to your previous link: would this: AppDomain.CurrentDomain.BaseDirectory is probably the most useful for accessing files whose location is relative to the application install directory. In an ASP.NET application, this will be the application root directory, not the bin subfolder - which is probably what you usually want. In a client app, it will be the directory containing the main executable.| allow me to code it as such without having to go through the C:\\... drive and all and would allow me to say start.process("TestApp\\Test.txt") – Daedric Apr 01 '15 at 06:24

4 Answers4

3

If you know that your app is going to be run from the same place every time (like a folder) you can call the GetCurrentDirectory() method. This will return a string of the current directory that your app is running from.

String pwd = GetCurrentDirectory(); //Contains something like C:\Users\Daedric\TestApp\
String finalString = Path.Combine(pwd, "test.txt"); //As per Corak
Mathemats
  • 1,185
  • 4
  • 22
  • 35
  • Please use [System.IO.Path.Combine](https://msdn.microsoft.com/library/fyy7a5kt.aspx) to combine paths. Don't use string concatenation. – Corak Apr 01 '15 at 06:25
  • @Corak Added, I couldn't remember is GetCurrentDirectory gave you a trailing slash or not, now I don't need to! – Mathemats Apr 01 '15 at 06:28
  • "now I don't need to!" - exactly. ^_^ – Corak Apr 01 '15 at 06:30
  • So I would then do something like this: String pwd = GetCurrentDirectory(C:\\Users\\Daedric\\TestApp\ String finalString = System.IO.Path.Combine(pwd, "test.txt"); Process.Start(finalString); – Daedric Apr 01 '15 at 06:31
  • No my friend, read the linked page. GetCurrentDirectory() returns the current directory that the exe is running from. It is how I have written it. @Daedric – Mathemats Apr 01 '15 at 06:33
  • Ah, I get it now. Sorry about that. I'm teaching my self on how to use C# as well as creating software using MS Visual Studio. Thank you guys for your help! – Daedric Apr 01 '15 at 06:38
  • Woohoo, I have stanic rep! – Mathemats Apr 01 '15 at 06:38
  • haha one last question, so I use the code like you have written, how do I use it in the Process.Start(); to open said file once the button is clicked? I know you said not to use it like I said earlier, but what then do I put in the Process.Start();? – Daedric Apr 01 '15 at 06:41
  • Sorry for all of the questions :/ – Daedric Apr 01 '15 at 06:41
  • `Process.Start(finalString);` is fine, just the stuff before that didn't make sense. – Corak Apr 01 '15 at 06:43
  • I think the current directory can be altered by the application so GetCurrentDirectory will not always be reliable. Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) will get the folder that the executable is running in. – Lee Willis Apr 02 '15 at 10:45
  • @LeeWillis GetCurrentDir() always returns the location that the exe is running from, what do you mean the application can alter the location? – Mathemats Apr 02 '15 at 10:55
  • SetCurrentDirectory will change the value returned. The documentation for GetCurrentDirectory states - "The current directory is distinct from the original directory, which is the one from which the process was started." – Lee Willis Apr 02 '15 at 10:58
1

You need start file from your app folder?

Application.StartupPath

for start file

Process.Start(Application.StartupPath + @"\test.txt");
GRUNGER
  • 486
  • 3
  • 14
0

Besides above answer, following example can help you understand how Path works:

class Program
    {
        static void Main()
        {
        string[] pages = new string[]
        {
            "cat.aspx",
            "really-long-page.aspx",
            "test.aspx",
            "invalid-page",
            "something-else.aspx",
            "Content/Rat.aspx",
            "http://dotnetperls.com/Cat/Mouse.aspx",
            "C:\\Windows\\File.txt",
            "C:\\Word-2007.docx"
        };
        foreach (string page in pages)
        {
            string name = Path.GetFileName(page);
            string nameKey = Path.GetFileNameWithoutExtension(page);
            string directory = Path.GetDirectoryName(page);
            //
            // Display the Path strings we extracted.
            //
            Console.WriteLine("{0}, {1}, {2}, {3}",
            page, name, nameKey, directory);
        }
        }
    }

Sample output would be like this:

Input C:\Windows\File.txt GetFileName: File.txt GetDirectoryName: C:\Windows

Ebad Masood
  • 2,389
  • 28
  • 46
0

GetCurrentDirectory is not the correct method to use as the return value can change while you application is running. You can see this by simply running the following in a console app:

Console.WriteLine(Directory.GetCurrentDirectory());
Directory.SetCurrentDirectory(@"c:\temp\");
Console.WriteLine(Directory.GetCurrentDirectory());

You can use the following to give you the assembly location:

Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Lee Willis
  • 1,552
  • 9
  • 14