Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,
I use VDS library for working with .RDF files. So i want to load .rdf file from my disk. My code is:

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


Where is:
C#
static IGraph g = new Graph();


and fuRDF is FileUpload from form.

When i load file like that:
C#
g.LoadFromFile("C:\\Users\\...\\kv.rdf");

then is loading file successfully, but when i load file with string, like that:
C#
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
Posted

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
 
Share this answer
 
Well...yes. Why are you surprised?

When you specify a folder directly:
C#
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
C#
MapPath("myFile.rdf")

will give you the equivilent of:
C#
@".\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:
C#
MapPath(@"~\Files\RDF\myFile.rdf")
Which will return a "Proper" path based on your website folder structure.
 
Share this answer
 
Thank you both. My solution is:
C#
fuRDF.SaveAs(Server.MapPath("~/dat.rdf"));
string filePathDat= HttpContext.Current.Server.MapPath("~/dat.rdf");
g.LoadFromFile(filePathDat);


Thanks again! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900