Specifying relative file location in web.config for use by standard C# class library

I’m struggling to find a way of specifying a file location in web.config appSettings that avoids using hard-coded paths but allows a non-‘web aware’ C# library to find a file.

The C# library uses standard File.Open, File.Exists methods, etc. to operate on a data file, which is stored in my web application (ASP.NET MVC) tree, e.g. under:

contentdataMyDataFile.txt

Requirements:

  • I want to be able to specify my path like, e.g.:
        <appSettings>
this-->     <add key="MyFileLocation" value="~contentdataMyDataFile.txt" />
not -->     <add key="MyFileLocation" value="c:inetpubwwwrootfoocontentdataMyDataFile.txt" />
        </appSettings>
  • I don’t want the C# library to be aware of the web application it’s being used in, as it is used in other software, and the web application has no need to know about the configuration of the C# library so I don’t really want to pass config info between the layers if possible.

Any suggestions on how I can do this cleanly? Thanks!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could use Path.Combine to combine AppDomain.CurrentDomain.BaseDirectory and your relative path.

This will give you a path relative to the ASP.NET root directory (~/) in an ASP.NET app, or a path relative to the directory containing the executable in a WinForms or Console application.

Method 2

Why not have the web application read the path from the config file, resolve it using Server.MapPath, then pass the resulting path to the class library?


Based on your comment, I have a different suggestion: don’t use relative paths.

Method 3

You could also do this:

In web.config:

<add key="MyFileLocation" value="content/dataMyDataFile.txt" />

In your code:

    string filePath = ConfigurationManager.AppSettings["MyFileLocation"];
    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath)

The result will give something like: “C:yourprojcontentdataMyDataFile” or something like that.

Method 4

For instance

In your web.config

    <appSettings>
        <add key="FilePath" value="~/images"/>
    </appSettings>

and In your code behind .cs file

    string filters = "*.jpg;*.png;*.gif";
    string Path = ConfigurationManager.AppSettings["FilePath"].ToString();

    List<String> images = new List<string>();

    foreach (string filter in filters.Split(';'))
    {
        FileInfo[] fit = new DirectoryInfo(this.Server.MapPath("~/images")).GetFiles(filter);
        foreach (FileInfo fi in fit)
        {
            images.Add(String.Format(Path + "/{0}", fi));                 
        }
    }

    RepeaterImages.DataSource = images;
    RepeaterImages.DataBind();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x