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 8661561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:31:32+00:00 2026-06-12T16:31:32+00:00

Consider this simple program: private static void Main(string[] args) { var directoryName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),

  • 0

Consider this simple program:

private static void Main(string[] args)
{
        var directoryName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Directory");

        if (Directory.Exists(directoryName))
            Directory.Delete(directoryName, true);

        Directory.CreateDirectory(directoryName);

        var stream = File.Create(Path.Combine(directoryName, "File")); //throws
        stream.Close();
}

This works fine while you simply execute this program. The strange thing happens if you browse that Directory in windows explorer and then run. In this case I get UnautorizedAccessException "Access to the path 'C:\Users\rfurman\AppData\Roaming\Directory\File' is denied."

If this is strange then execute this with the same conditions:

private static void Main(string[] args)
{
        var directoryName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Directory");

        if (Directory.Exists(directoryName))
            Directory.Delete(directoryName, true);

        var value = Directory.Exists(directoryName);

        Console.WriteLine(value);
        Console.ReadKey();
 }

This program prints True if Directory is open in explorer.

What I would like to know is why this happens and how to defend against such situation.

I use windows 7 and .net 4.

  • 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-06-12T16:31:33+00:00Added an answer on June 12, 2026 at 4:31 pm

    Directory.Delete internally uses RemoveDirectory win api in Kernel32. What RemoveDirectory does is “to mark directory for deletion”. Directory is deleted when last handle of that directory is closed. I believe this means “after explorer left that folder”

    In my computer this situation does not occur, so I cannot test but I suspect there may be a way for you. NT based systems sometimes allows renaming of files and directories even if they are open. I don’t know exact cases this is allowed, but I used this to rename loaded dll files and write new ones like this:

    File.Rename(@"C:\App\test.dll", @"C:\App\test.dll");
    File.Copy(@"C:\App\Update\test.dll-v1.1", @"C:\App\test.dll");
    

    So your code may look like this after change

    var directoryName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Directory");
    
    if (Directory.Exists(directoryName)) {
        var randomExt = ".random"; // generate randomly
        Directory.Move(directoryName, directoryName + randomExt)
        Directory.Delete(directoryName + randomExt, true);
    }
    Directory.CreateDirectory(directoryName);
    
    var stream = File.Create(Path.Combine(directoryName, "File")); //throws
    stream.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: class Program { static void Main(string[] args) { Department deathStar
Let's consider this simple test program: #include <stdio.h> #include <string.h> int main(int argc, char
Consider this simple example - public class Person { private String name; private Date
Consider this simple console application: using System; namespace Demo { class Program { static
Consider this simple program. Can you explain why the output is different after uncommenting
Consider this simple program. The program has two files: File Vehicle.java class Vehicle {
Consider a simple program in C++: #include <iostream> class link {}; int main() {
Consider this simple program: class Shape { public: virtual double getArea() = 0; };
Consider this simple benchmark: n :: Int n = 1000 main = do print
Consider the program: #include <gc/gc.h> int main() { void* p = GC_MALLOC(15); } Under

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.