Answered Using path.GetFullPath to return local directory?

Ren6175

Member
Joined
Aug 5, 2019
Messages
22
Programming Experience
Beginner
I’ve received a lot of great help previously on this site. I have another question. I have a program that loads images from a path on my computer dynamically. Obviously if I copy this program to another computer the path will be different. I need to find the root directory of the program dynamically so that I can tell people where to put their files then my program can load them properly. Is that the function of path.GetFullPath or is there another way of doing this?
 
Or the C# way of doing the same as the VB linked to above...
 
You should be giving your users an open file dialog if they need to select files, or an open folder dialog so they can select a path.

Programmatically you can use these 3 different versions will return your executables path.

C#:
        private void Button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine(string.Concat(GetExecutingPath(), @"\"));

            Console.WriteLine(string.Concat(GetExecutingFilesFolder(), @"\"));

            Console.WriteLine(GetExecutingDir());
        }
        public string GetExecutingPath()
        {
                string myExe = Assembly.GetExecutingAssembly().CodeBase;
                UriBuilder buildUri = new UriBuilder(myExe);
                string location = Uri.UnescapeDataString(buildUri.Path);
                return Path.GetDirectoryName(location);
        }
        public string GetExecutingDir()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }
        public string GetExecutingFilesFolder()
        {
            return Environment.CurrentDirectory;
        }
If you don't string concatenate the trailing "\" they will output like this :
C#:
F:\All Storage\2016 Projects\Apps\TestApps\Test\TestCSharpApp\TestCSharpApp\bin\Debug
F:\All Storage\2016 Projects\Apps\TestApps\Test\TestCSharpApp\TestCSharpApp\bin\Debug
F:\All Storage\2016 Projects\Apps\TestApps\Test\TestCSharpApp\TestCSharpApp\bin\Debug\
The concatenation just adds the trailing slash. While I think its a bad idea dumping files into your application root folder, John's post would be what I would advise you to follow. But since you asked. Any of those will work for you.
 
string concatenate the trailing "\"
I prefer Path.Combine for that, if I need to add together parts, it handles the directory separator when needed.
 
Well you can use either JohnH, but yes, I agree, it is programmatically correct to use Path.Combine when dealing with windows paths, instead of String.Concat.
OP : With Path.Combine, you also don't need a literal string to filter the blackslash as the backslash is not necessary when using this method. You will also see that I've applied JohnH's concept above for using the pictures folder by acquiring it with Environment.SpecialFolder, and providing an example of when you might use Path.GetFullPath.
C#:
        private void Button2_Click(object sender, EventArgs e)
        {
            string mainPath = (Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
            string personalFolder = "myfolder";
            string fullPath = Path.GetFullPath(Path.Combine(mainPath, personalFolder));
            if (Directory.Exists(fullPath) ? false : true)
            {
                Directory.CreateDirectory(fullPath);
                Console.WriteLine(fullPath);
            }
        }
However you want to build your paths really is up to you. If you wanted, you could do it by string building. And it really doesn't matter once you know what you're doing and once they both output the same result. Which the above and below do. That being said, Microsoft have specifically given us methods to use for these exact purposes. While the first snipped is Ok, you could write it the same way with only two lines. See third example snipped. As for the second example; while it's not using the structural methods designed for what we want to achieve on our third snipped, it does perform just as well and also does the exact same job. :)
C#:
        private void Button1_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
            int position = sb.Length;
            string folder = @"\myfolder";
            sb.Insert(position, folder);
            if (Directory.Exists(sb.ToString()) ? false : true)
            {
                Directory.CreateDirectory(sb.ToString());
                Console.WriteLine(sb.ToString());
            }
        }
But why write all that code when you can do it in two lines.
C#:
        private void Button3_Click(object sender, EventArgs e)
        {
            string fullpath = Path.GetFullPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "myfolder"));
            if (Directory.Exists(fullpath) ? false : true) Directory.CreateDirectory(fullpath);
        }
 
Last edited:
string fullPath = Path.GetFullPath(Path.Combine(mainPath, personalFolder));
GetFullPath is not necessary here, the combination is already a fully qualified path.
 
What on earth. My button 2 does not look like that. :S
C#:
        private void Button2_Click(object sender, EventArgs e)
        {
            string fullpath = Path.GetFullPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "myfolder")));
            Console.WriteLine(fullpath);
            if (Directory.Exists(fullpath) ? false : true) Directory.CreateDirectory(fullpath);
        }
Something got screwed up there. What that should have been is a the snipped above. Actually, you're right, my error, I forgot a bracket!! :ROFLMAO: It's not my night tonight.
Screenshot_28.jpg


I also fixed some missing brackets on post 7.
 
Took me a second to workout why intelisense was trying to convert string to System.Environment....., then I noticed the missing closing bracket as seen in the screenshot. Silly me, I don't function very well in this hot weather at all. The proof is there lol

To example the usage of Path.GetFullPath with some output.
C#:
        private void Button4_Click(object sender, EventArgs e)
        {
            string dirC = @"dirC";
            string dirD = @"\dirD";
            Console.WriteLine(@"\\MyServer\Network\..\..\LastDirectory");
            Console.WriteLine(Path.GetFullPath(@"\\MyServer\Network\Path1\..\..\LastDirectory"));
            Console.WriteLine(Path.GetFullPath(dirC + dirD));
            Console.WriteLine(Path.GetFullPath(dirD + dirC));
            Console.WriteLine(Path.GetFullPath(dirD + "\\" + dirC));
        }
Output:
\\MyServer\Network\..\..\LastDirectory
\\MyServer\Network\LastDirectory
F:\All Storage\2016 Projects\Apps\TestApps\Test\TestCSharpApp\TestCSharpApp\bin\Debug\dirC\dirD
F:\dirDdirC
F:\dirD\dirC
As an aside, I mostly write it into my paths by habit as I am almost always nearly working with unqualified paths which need to be corrected. How I applied it above (#7) will just make the compiler disregard it since there is nothing to qualify. But what I was trying to point out; often it is normally the last method to wrap a formatted path, where the uri is not a qualified mapped path.

Good night for now ;)
 
Back
Top Bottom