Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

WCF service has one method ( Let's say TestMethod) in which I try to create a File Stream like this :

            System.IO.FileStream fs = new System.IO.FileStream(@"D:\Test.xml", System.IO.FileMode.Open);

My Client and Service is on the same solution.

When the Client makes a call to TestMethod ( Exposed in Web service ) it will give this error:

Access to the path 'D:\DXDirectoryAuth.xml' is denied.

Please Help!!

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Have you tried:

System.IO.FileStream fs = new System.IO.FileStream(@"D:\Test.xml", System.IO.FileMode.Open, System.IO.FileAccess.Read);

The default constructor of FileStream() asks for read and write access.

share|improve this answer
    
It's not very intuitive. Changing file access mode solved my problem too. Thanks! –  Doug May 17 '12 at 15:38

Security!

The reason being you are trying to access a file location outside of the directory where you have hosted your WCF service...

You are either going to have to grant the account the WCF runs under permissions to that directory or move the file into the directory\sub-directory where you are hosting the WCF service.

Ollie

share|improve this answer
    
Thanks Ollie.. can you please guide me how can I achieve this?? My Service folder is "MyService", I have put the xml file in MyService folder.. but still it is giving this error –  Ashish Ashu Sep 2 '09 at 9:50

Okay, if you have put the file in the directory or a sub-directory of your WCF service you should be able to access the file without any permissions issues.

The question is how are you attempting to access the file?

You should probably get the current directory of the service then append the relative file location onto the current directory and then attempt to open the file something like this:

var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
bodyFile = Path.Combine(appPath, @"templates\email.txt");

var body = File.OpenText(bodyFile).ReadToEnd();

HTH

Ollie

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.