views:

2085

answers:

6

I have a Visual Studio 2008 solution that contains a handful of projects. One project contains a WCF Service I'm deploying. That WCF Service references some code in one of the other projects. That code is trying to read a file that's in a folder in the WCF project. Pseudo-project structure:

Solution
 Project1
  myclass.cs
    string file = Server.Mappath("");


 Project2
  filefolder
    myfile.txt

What is the correct syntax to put in the Mappath? I've tried all different variations such as:

".filefolder/myfile.txt"
"/filefolder/myfile.txt"
"./filefolder/myfile.txt"
"~/filefolder/myfile.txt"

None seem to be able to reach the file. One thing I thought of: Visual Studio 2008 runs the project and WCF in its own sandbox in IIS. Could that be the issue? Would it work if setup and deployed in regular IIS?

A: 

The best thing would be to not do this to begin with.

Why would you want to tie the deployment location of one project to the deployment location of another? If you need the WCF service to read the file, then copy the file to the WCF service.

John Saunders
Project1 contains all the data access and business objects that I dont want exposed to the customer buying the WCF (Project2). All they are getting is the WCF project with the endpoints and some dlls.
Blaze
And yet, the WCF service needs access to some file from the DAL/BLL project. Do those projects need the file as well? In any case, either put the files in the same folder as the WCF service, or else put the path to the file in the config file of the WCF project.
John Saunders
Its confusing I know. The file is stored in the WCF project (2). The DAL/BLL project (1) needs to read the file, do some operations with it, and pass the result to the WCF for going out the endpoint.
Blaze
A: 

From MSDN; Because the path parameters in the following examples do not start with a slash character, they are mapped relative to the directory that contains the example file.

Try:

Server.Mappath("filefolder/somefile.file");
Jreeter
This did not work either, I think I tried every possible path!
Blaze
+1  A: 

Have you tried using HostingEnvironment.ApplicationPhysicalPath?

var fileInfo = new FileInfo(
    Path.Combine( new DirectoryInfo( HostingEnvironment.ApplicationPhysicalPath ).Parent.Name , @"filefolder/myfile.txt" ) );
Steve Dignan
+1  A: 

The problem is that when invoking the WCF, the file system runs all the way out to the bin/Debug folder. So trying to MapMath from there doesnt work. Backtracking up the path worked:

filedata = File.ReadAllBytes("../../filefolder/myfile.txt");

That worked. Thanks for all the help guys!

Blaze
A: 
Server.MapPath(Path.Combine( new DirectoryInfo( HostingEnvironment.ApplicationPhysicalPath ).Parent.Name , "Filename.txt" ));

Seems to work for me. I did need to include

using System.Web.Hosting;
corymathews
A: 
var serverPath =
       System.Web.Hosting.HostingEnvironment.MapPath("~/filefolder/myfile.txt");
wildcat