在web.config中指定相对文件位置,以由标准的C#类库使用 [英] Specifying relative file location in web.config for use by standard C# class library

查看:147
本文介绍了在web.config中指定相对文件位置,以由标准的C#类库使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力寻找在避免使用硬codeD路径的web.config 的appSettings指定文件位置的一种方式,但允许非网络知道'C#库来查找文件。

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.

在C#库使用标准的 File.Open File.Exists 方法等对数据进行操作文件,该文件保存在我的Web应用程序(ASP.NET MVC)树,如下:

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:

\content\data\MyDataFile.txt

要求:


  • 我希望能够指定我的道路一样,例如:


        <appSettings>
this-->     <add key="MyFileLocation" value="~\content\data\MyDataFile.txt" />
not -->     <add key="MyFileLocation" value="c:\inetpub\wwwroot\foo\content\data\MyDataFile.txt" />
        </appSettings>


  • 我不希望C#库要意识到它在正在使用的Web应用程序,因为它是在其他软件中使用,Web应用程序已经没有必要知道关于C#库的配置,所以我不要'吨真的想如果可能的话传递层之间的配置信息。

  • 我如何能做到这一点干净有什么建议?谢谢!

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

    推荐答案

    您可以使用 Path.Combine 合并 AppDomain.CurrentDomain.BaseDirectory 和你的相对路径。

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

    这会给你一个相对路径到ASP.NET根目录(〜/)在一个ASP.NET应用程序,或相对于包含在一个WinForms或控制台应用程序的可执行文件的目录的路径。

    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.

    这篇关于在web.config中指定相对文件位置,以由标准的C#类库使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆