读取提供 .NET 相对路径的文件中的所有行 (F#)
我正在 .NET 库中寻找一种方法(我猜它是静态方法),让我在读取文件时指定相对路径。 我的代码是:
let mycontent = Syste..IO.File.ReadAllLines ("C:\...\myfile.txt")
但是我想指定一个相对路径。怎样做?我猜它是某种MapPath
......
I am looking for a method (I guess it is a static method) in the .NET libraries to let me specify a relative path when reading a file.
My code is:
let mycontent = Syste..IO.File.ReadAllLines ("C:\...\myfile.txt")
However I would like to specify a relative path. How to? It is some sort of MapPath
I guess...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
相对路径的指定方式如下:
它将相对于执行应用程序的工作目录搜索
myfile.txt
。它也适用于子文件夹:您在问题中引用的
MapPath
函数用于 ASP.NET 应用程序,并允许您在给定虚拟路径的情况下检索文件的绝对路径。例如:如果您的网站托管在
c:\wwwroot\mysite
中,它将返回c:\wwwroot\mysite\foo\myfile.txt
。A relative path is specified like this:
which will search
myfile.txt
relative to the working directory executing your application. It works also with subfolders:The
MapPath
function you are referring to in your question is used in ASP.NET applications and allows you to retrieve the absolute path of a file given it's virtual path. For example:and if your site is hosted in
c:\wwwroot\mysite
it will returnc:\wwwroot\mysite\foo\myfile.txt
.如果您想要相对于应用程序所在目录的路径(而不是当前工作目录,可能不同),您可以执行以下操作:
If you want paths relative to the directory in which your application resides (as opposed to the current working directory, which may be different), you can do the following:
您可以将相对路径传递给任何接受文件路径的函数,并且它会正常工作。
You can pass a relative path to any function that accepts a file path, and it will work fine.