C# relative, absolute path

Markus Freitag 3,791 Reputation points
2021-01-22T12:56:26.537+00:00

Hello,
59621-a.png

59489-b.png

why is not workling?
I use relative path.

ToXMLFile($@"..\..\Data\Orders\{CurrentOrderSerials.OrderId}.xml");  

  public static void ToXMLFile<T>(this T obj, string filePath, bool omitDeclaration = true) where T : class  
        {  
            using (StreamWriter writer = new StreamWriter(filePath))  

C:_Prog\XXX\APP\Data\Orders

I expect two levels higher, not from bin folder.

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,521 Reputation points
    2021-01-26T08:08:46.583+00:00

    You can use Path.GetFullPath with a relative path =>

    string sCurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;              
    string sFile = System.IO.Path.Combine(sCurrentDirectory, @"..\..\..\Data\Orders\Test.xml");  
    string sFilePath = Path.GetFullPath(sFile);  
    
    3 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,586 Reputation points
    2021-01-22T13:54:06.31+00:00

    Just think about it: two levels up could be multiple directories. So which one to choose?
    You need to provide a fully qualified path.

    This is how it is usually done. The System.IO.Path contains multiple useful methods to handle directory and file processing.

    void Main()
    {
     const string baseDir = @"e:\temp";
    
     string fileName = "mynewfile.xml";
    
     string fullyQualifiedFileName = Path.Combine(baseDir, fileName);
     Console.WriteLine("Fully qualified file name: '{0}'", fullyQualifiedFileName);
    }
    
    1 person found this answer helpful.

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-01-22T17:52:19.627+00:00

    If you need the level then consider the following which given a level gets a parent folder according to the level provided so then combine that path with a file name, use File.Exists etc.

    Helper class

    using System;
    using System.Collections.Generic;
    using System.IO;
    
    namespace Utilities.Classes 
    {
        public static class DirectoryHelper
        {
            public static string UpperFolder(int level, string folderName = "")
            {
                if (string.IsNullOrWhiteSpace(folderName))
                {
                    folderName = AppDomain.CurrentDomain.BaseDirectory;
                }
    
                var folderList = new List<string>();
    
                while (!string.IsNullOrWhiteSpace(folderName))
                {
                    var parentFolder = Directory.GetParent(folderName);
                    if (parentFolder == null)
                    {
                        break;
                    }
    
                    folderName = Directory.GetParent(folderName).FullName;
                    folderList.Add(folderName);
                }
    
                if (folderList.Count > 0 && level > 0)
                {
                    return level - 1 <= folderList.Count - 1 ? folderList[level - 1] : folderName;
                }
    
                return folderName;
            }
    
            public static string CurrentSolutionFolder() => UpperFolder(4);
        }
    }
    

    Form code

    Add this using statement

    using static Utilities.Classes.DirectoryHelper;
    

    Simple usage

    Debug.WriteLine(CurrentSolutionFolder());
    Debug.WriteLine("");
    for (int index = 0; index < 5; index++)
    {
        Debug.WriteLine($"{index}\t{UpperFolder(index)}");
    }
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.