C# Programming > Data

C# Working with Relative Paths

 

Relative Paths in .NET

For .NET applications that offer a dialog box with an input to enter a file, it is convenient that the file name is manipulated with relative path for the reason of presenting the file in the screen (Text boxes or the From Text itself).

If the file was displayed with a full path, the string representing the field can be very long and impractical to handle. 

Changing full path to a relative path

With this idea in mind, consider that a dialog box offers the user the option to select a file that represents some data, and the file is located in the starting folder of the session. For example 

 

As the user selects the File button, a file selection box appears, and after choosing the file, the OpenFileDialog brings the file name with fully qualified path.

The System.Windows.Forms offers an attribute that holds the starting folder of the application (Application.StartupPath).  Thinking that the full path of the file is equal to the Starting folder then we can save the filename with no path. If the selected file is located in a folder under the startup folder, the file name can be stored using relative indications (e.g. “\MyFile.txt”).

The code that depicts the idea is as follows,

// Create and open a file selector
OpenFileDialog opnDlg = new OpenFileDialog();
opnDlg.InitialDirectory = ".";

// select filter type
opnDlg.Filter = "Parm Files (*.parm)|*.parm|All Files (*.*)|*.*";

if (opnDlg.ShowDialog(this) == DialogResult.OK)
{
    // If file is in start folder remove path
    FileInfo f = new FileInfo(opnDlg.FileName);
   
    // use relative path if under application
    // starting directory
    if (f.DirectoryName == Application.StartupPath)
        txtFilename.Text = f.Name;  // only file name
    else if (f.DirectoryName.StartsWith(Application.StartupPath))
      // relative path
      txtFilename.Text = f.FullName.Replace(Application.StartupPath, ".");
    else
       txtFilename.Text = f.FullName;  // Full path
}

Restoring Full Path

Right before opening the file, it becomes necessary to convert the relative path to a full qualified path to avoid errors. The idea is to examine the name of the file, and base on the application StartupPath attribute, reconstruct the full path. The following code depicts the idea (the System.Windows.Forms namespace is required), 

public static string GetFullFileNameFromApplication(string iFilename)
{
    string iNewFilename = "";
 
    // Get full name considering relative path
    FileInfo f = new FileInfo(iFilename);

    if (iFilename.StartsWith(".\\"))  // File in child folder
        iNewFilename = Application.StartupPath
                     + iFilename.Substring(1); // leave period out of string
    else if(!iFilename.Contains("\\")) // Consider file in StartupPath
        iNewFilename = Application.StartupPath
                     + "\\"
                     + iFilename;
   else
        iNewFilename = f.FullName; // keep full path

    return iNewFilename;
}

The first two if conditions handle the criteria for relative path established. In case that the file path is from a location away from the startup Path, the full path is respected with no change.

Back to C# Article List