-1

For a web app in asp.net, I have to open a file located in my "app_data" directory. Locally, it works fine.

But when published on the server, I have an error that say : "The system cannot find the file specified"

Here is my code :

StreamReader reader = new streamReader(Server.MapPath("~/App_Data/pubkey.pem"));

Of course, the file exists on the server.

What am I doing wrong ?

Thanks ;)

EDIT : Since the error was not where I think she was, my problem is solved. I have marked mike's response as valid because it helped me a lot to find out I was wrong :)

5
  • try without the ~, if that doesn't work then check where you are actually calling this, maybe you need to add ../../ but I doubt that since it is working locally, I know App_Data is a protected folder so maybe this is somehow blocking you, when publishing make sure to include the App-Data folder
    – CularBytes
    Jun 7, 2018 at 19:21
  • is that file actually there? Jun 7, 2018 at 19:39
  • Did you on server add App_Data as a Virtual folder on IIS? Looks to me like that's the issue. Go to IIS->[Your web app] expand and check if folder is there. If it isn't just right click -> Add Virtual Directory and add path to the folder
    – Caldazar
    Jun 7, 2018 at 20:48
  • For @CularBytes thanks but same without the ~and the appdata folder is included.
    – like73
    Jun 8, 2018 at 6:02
  • @DanielA.White yes it is
    – like73
    Jun 8, 2018 at 6:02

1 Answer 1

1

You will need a App_Data folder defined as a virtual folder in IIS, or a folder in your website project called App_Data, as the ~ says go to the root of website. If your looking for the App_Data folder present in a user profile in windows then it's the virtual folder problem, or/and giving the app pool identity the proper permissions to access the folder.

try
        {   // Open the text file using a stream reader.
            using (StreamReader sr = new StreamReader(Server.MapPath("~/App_Data/pubkey.pem")))
            {
            // Read the stream to a string, and write the string to the console.
                string doc = sr.ReadToEnd();
                System.Diagnostics.Debug.WriteLine(doc);
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("The file could not be read:");
            System.Diagnostics.Debug.WriteLine(e.Message);
        }
6
  • Ok thank you but I can't define a virtual folder since my web app is hosted on a 1&1 windows unlimited hosting I have no control over IIS... I tried to put my file on the root of the website but same. I don't know if it can help but I spied the Server.MapPath("~/App_Data/pubkey.pem") and the value is : E:\kunden\homepages\27\d732552085\www\App_Data\pubkey.pem I have checked the security settings of this folder and the IUSR has read access
    – like73
    Jun 8, 2018 at 6:17
  • What do you get when you do this: bool exists =System.IO.File.Exists(Server.MapPath("~/App_Data/pubkey.pem"));
    – Mike
    Jun 8, 2018 at 12:08
  • I got true... :/
    – like73
    Jun 8, 2018 at 16:37
  • Ok i added another test to my original answer, see what happens when you run it. If that fails , maybe the exception will tell you whats going on. Since the previous test came back true. It is not a locating/access issue.
    – Mike
    Jun 8, 2018 at 17:38
  • Try right clicking on the file(not the App_Data folder), select properties, then look at the properties window and check the Build Action, should be set to Content. Also gives you the option on when to Copy to Output Directory. (ie if its newer then deployed version)
    – Mike
    Jun 8, 2018 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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