Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3225590
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:21:25+00:00 2026-05-17T16:21:25+00:00

Can we depend on the current working directory in ASP.NET code-behinds? Or, in other

  • 0

Can we depend on the current working directory in ASP.NET code-behinds? Or, in other words, can we use relative paths, and be sure that they’ll work?

If, in one page on a website, I set the current working directory to something specific, will it still be the same the next time another page on the website is loaded? When the same page on the website is loaded?

If I set the current working directory to something specific, in Page_Load(), can I be sure that it will still be the same by the time Page_PreRender() is called? Or could another page on the same website change it on me, in between? Could a page on a different website in the same application pool change it on me? A page in a different website in a different app pool?

In other words, what is the scope of the current working directory, in IIS? Is it specific to a page? Is it specific to a web site? Or is it shared among all pages in an app pool?

Where, among page, website, app pool, and server, are the boundaries that isolate different values of current working directory?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:21:26+00:00Added an answer on May 17, 2026 at 4:21 pm

    Environment.CurrentDirectory is a simple wrapper around the GetCurrentDirectory and SetCurrentDirectory winapi functions. Indeed, trying to set the directory requires UnmanagedCode permissions. Whenever a function prevents your site from running in partial trust, you are right to be wary of depending on it. 🙂

    From the SetCurrentDirectory documentation:

    Changes the current directory for the current process.

    The best explanation I can find that covers the relationship between the w3wp.exe process and an ASP.NET site is this answer. Any other page within your site could potentially change your page’s current working directory. Any pages on any other site under the same application pool could potentially change your page’s current working directory. These outside changes to the current working directory could happen at any time during your page’s execution. On the other hand, a page on a site under a different application pool will not change your page’s current working directory. The reason I say “could potentially” is that it gets even more complicated if you consider web garden scenarios, where there can be more than one process for a single ASP.NET site.

    Now consider that SetCurrentDirectory is not thread safe:

    Multithreaded applications and shared
    library code should not use the
    SetCurrentDirectory function and
    should avoid using relative path
    names. The current directory state
    written by the SetCurrentDirectory
    function is stored as a global
    variable in each process, therefore
    multithreaded applications cannot
    reliably use this value without
    possible data corruption from other
    threads that may also be reading or
    setting this value. This limitation
    also applies to the
    GetCurrentDirectory and
    GetFullPathName functions. The
    exception being when the application
    is guaranteed to be running in a
    single thread, for example parsing
    file names from the command line
    argument string in the main thread
    prior to creating any additional
    threads. Using relative path names in
    multithreaded applications or shared
    library code can yield unpredictable
    results and is not supported.

    Chances are that you don’t want to depend on the current working directory. Having said that, given how foolish it is to rely on the current working directory, you can be reasonably certain that no other code will be touching it. 🙂 A quick peek with Reflector shows that no .NET framework code changes it. A few functions do check it though, so watch out for those. If you control the deployment environment, you can ensure that your site runs in its own application pool. With proper synchronization technique, you should then be able to safely update the current working directory. I wouldn’t consider it anything other than a hack though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can you depend on test code from another module in Maven? Example, I
can you recommend some good ASP.NET tutorials or a good book? Should I jump
I have this code: http://jsfiddle.net/5RbrL/ As you can see, the text doesn't go over
When using IDbCommand, IDataReader, or DataTable, can you depend on the destructor to dispose
Can a LINQ enabled app run on a machine that only has the .NET
Can I get a 'when to use' for these and others? <% %> <%#
I'm current on POS project. User require this application can work both online and
Can i get the source code for a WAMP stack installer somewhere? Any help
I want to use a temp directory that will be unique to this build.
I'm currently working on a project which is using EF Code First with POCOs.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.