如何在LoadFromFile方法中正确使用路径? [英] How to correctly use path in LoadFromFile method?

查看:447
本文介绍了如何在LoadFromFile方法中正确使用路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用VDS库来处理.RDF文件。所以我想从我的磁盘加载.rdf文件。我的代码是:



  string  filePathDat = Path.GetFullPath(Server .MapPath( this  .fuRDF.FileName)); 
g.LoadFromFile(filePathDat);





其中:

 静态 IGraph g =  new  Graph(); 





和fuRDF是来自表单的FileUpload。



当我加载这样的文件时:

 g.LoadFromFile(  C:\\Users\\。 .. \\kv.rdf); 



然后成功加载文件,但是当我用字符串加载文件时,就像那样:

 g.LoadFromFile(filePathDat); 



加载文件失败。在视觉工作室,我一步一步地看着差异。两条路都是一样的。



http://postimg.org/image/f3ljggmln/



有没有人有任何想法解决这个问题?谢谢

解决方案

这是因为你错误地映射了它。请注意,在一种情况下,您从根驱动器目录开始路径,但 Server.MapPath 从为您的站点配置的根路径开始映射它。



此外,您的路径如C:\ Users \ ...完全无效,至少有两个原因:

  1. 此路径不可移植。您需要使用特殊文件夹才能使其合法化:

    http: //msdn.microsoft.com/en-us/library/14tx8hby.aspx [ ^ ],

    如何查找我的程序目录 [ ^ ]。

  2. 无论如何,你不能在Web应用程序中使用上面获得的路径。它们在特殊的沙盒环境中执行,不允许您访问为站点配置的根目录之外的任何文件系统对象。这与许可无关;无论你做什么,你都无法访问这些目录。





因此,唯一合法的方法是使用使用Server.Mappath 。只能从网站的根目录正确映射,而不是磁盘。



-SA


嗯......是的。你为什么感到惊讶?



当你直接指定一个文件夹时:

 g.LoadFromFile(  C:\\Users\\ ... \\kv.rdf) ; 

你说的是文件的位置。

当你从文件上传控件通过MapPath引用它时,你得到的只是文件名:没有路径信息(因为你根本无法访问客户端硬盘驱动器,因此出于安全原因不会传输文件夹信息。



所以

< pre lang =c#> MapPath( myFile.rdf



会给你相同的:

  @ 。\ myFile.rdf 



其中。是当前网页的位置定义的当前文件夹。如果你没有;明确地将文件保存在那里,将找不到该文件...

最有可能的是,你想使用类似的东西:

 MapPath( @ 〜\Files \RDF \ myFile.rdf

这将根据您的网站文件夹结构返回正确路径。


谢谢两者。我的解决方案是:

 fuRDF.SaveAs(Server.MapPath( 〜/ dat.rdf)); 
string filePathDat = HttpContext.Current.Server.MapPath( 〜/ dat.rdf);
g.LoadFromFile(filePathDat);





再次感谢! :)


Hi,
I use VDS library for working with .RDF files. So i want to load .rdf file from my disk. My code is:

string filePathDat = Path.GetFullPath(Server.MapPath(this.fuRDF.FileName));
g.LoadFromFile(filePathDat);



Where is:

static IGraph g = new Graph();



and fuRDF is FileUpload from form.

When i load file like that:

g.LoadFromFile("C:\\Users\\...\\kv.rdf");


then is loading file successfully, but when i load file with string, like that:

g.LoadFromFile(filePathDat);


is loading file unsuccessfully. In visual studio, i go step by step and watching the differences. Both path is the same.

http://postimg.org/image/f3ljggmln/

Does anyone have any idea for solution of this problem? Thanks

解决方案

This is because you map it incorrectly. Pay attention that in one case, you start your path from the root drive directory, but Server.MapPath maps it starting from the root path configured for your site.

Moreover, your path like "C:\Users\..." is totally invalid, by at least two reasons:

  1. This path is not portable. You would need to use "special folder" to make it legitimate:
    http://msdn.microsoft.com/en-us/library/14tx8hby.aspx[^],
    How to find my programs directory[^].
  2. Anyway, you cannot use the paths obtained as mentioned above in Web application. They are executed in a special sandboxed environment which does not give you access to any file system objects outiside the root directory configured for your site. This is not related to permission; you just don't have access to those directories, no matter what you do.



So, the only legitimate way is using Server.MapPath. Only map it correctly, from the root directory of the site, not the disk.

—SA


Well...yes. Why are you surprised?

When you specify a folder directly:

g.LoadFromFile("C:\\Users\\...\\kv.rdf");

you are saying where the file is.
When you reference it via MapPath from the File Upload control, all you get is teh file name: no path information (because you can't access the client hard drive at all and so the folder info isn't transferred fro security reasons).

So

MapPath("myFile.rdf")


will give you the equivilent of:

@".\myFile.rdf"


Where "." is the current folder as defined by the location of the current web page. If you haven;t explicitly saved the file there, the file will not be found...
Most likely, you want to use something like:

MapPath(@"~\Files\RDF\myFile.rdf")

Which will return a "Proper" path based on your website folder structure.


Thank you both. My solution is:

fuRDF.SaveAs(Server.MapPath("~/dat.rdf"));
string filePathDat= HttpContext.Current.Server.MapPath("~/dat.rdf");
g.LoadFromFile(filePathDat);



Thanks again! :)


这篇关于如何在LoadFromFile方法中正确使用路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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