Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6706035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:28:22+00:00 2026-05-26T07:28:22+00:00

We have a C# application which will write files to a configurable location. The

  • 0

We have a C# application which will write files to a configurable location. The set of files (and relative paths) is determined at runtime.

We want to ensure that it cannot write files outside the configured location.

For example, the configured location might be c:\Stuff\Export, it would be an error for the program to write anything under C:\Stuff\Important

Really, I think we can achieve this in two ways:
1) Assert none of the relative paths (files to be written) specify ‘Parent directory’ (typically “../”) – System.Path doesn’t specify a “parent directory” path component though (like it has for path separation i.e. System.Path.PathSeparator). I feel a bit cludgey checking for “../” in the string.

2) Assert that all of the final absolute paths that are generated (by combining the output location with the file relative path) are relative to i.e. underneath the output location. I’m not exactly sure how to go about this though.

Example usage:
Output directory: c:\Stuff\Export
Output path 1: "foo\bar\important.xls"
Output path 2: "foo\boo\something.csv"
Output path 3: "../../io.sys"

Expected final files
1. c:\Stuff\Export\foo\bar\important.xls
2. c:\Stuff\Export\foo\boo\something.csv
3. Should throw exception
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:28:23+00:00Added an answer on May 26, 2026 at 7:28 am

    If you create a DirectoryInfo instance on the two paths, its FullName property should return the fully qualified, canonical path. So if you just do that for both of the sides you want to compare, you can do this:

    if (chosenDirectory.FullName != configuredDirectory.FullName)
    {
        throw new InvalidOperationException(
            String.Format("Invalid path {0}.", chosenDirectory));
    }
    

    Since FullName is just a string, you can do regular string comparison on the paths, like:

    if (!chosenDirectory.FullName.StartsWith(configuredDirectory.FullName,
        StringComparison.InvariantCultureIgnoreCase))
    {
        throw new InvalidOperationException(
            String.Format("Invalid path {0}.", chosenDirectory));
    }
    

    You can also use the Parent property and compare its FullName to the chosen directory, if you don’t want to allow sub-directories within the configured directory:

    if (!chosenDirectory.Parent.FullName.Equals(configuredDirectory.FullName,
        StringComparison.InvariantCultureIgnoreCase))
    {
        throw new InvalidOperationException(
            String.Format("Invalid path {0}.", chosenDirectory));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a standalone Java application which will have a embedded HTTP
I want to create an application which will have a client and server components.
I want my application to have a zoomable element which will allow the XAML
I have to write an application, which will compare the modification date of two
I have an application which will use WCF to serve up various chunks of
I have a silverlight application which users will be running in various time zones
I'm designing an application which will have a network interface for feeding out large
I have an idea for a simple application which will monitor a group of
I'm looking at building a Rails application which will have some pretty large tables
I'm thinking how to arrange a deployed python application which will have a Executable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.