Nickolodeon
Nickolodeon

Reputation: 2956

Directory.CreateDirectory strange behaviour

I have a windows service which runs a separate thread with a function which may do

if (!Directory.Exists(TempUpdateDir))
        {
            DirectoryInfo di = Directory.CreateDirectory(TempUpdateDir);
            di.Refresh();
            EventLog.WriteEntry("Downloader", string.Format("DEBUG: Trying to create temp dir:{0}. Exists?{1},{2}",TempUpdateDir, Directory.Exists(TempUpdateDir), di.Exists));
        }

which does not throw exceptions, Directory.Exists says true (inside if block) and yet there is no such directory on the disk, when you look with explorer. I've seen directory created a couple of times, but most of the time directory isn't created, no exceptions thrown either.

(This service runs under Local System) Later on this service starts program using Process class and exits. That program is also suppose to work with files, copy them to created directory, but it doesn't do it either.

Code has problems on Windows 2003 server.

What the....?????????????

Upvotes: 0

Views: 2542

Answers (2)

arena-ru
arena-ru

Reputation: 1020

To create folders, create an instance of DirectoryInfo and then call the DirectoryInfo .Create method. You can check the boolean DirectoryInfo.Exists property to determine if a folder already exists. The following sample checks for the existence of a folder and creates it if it doesn’t already exist, although the Common Language Runtime (CLR) does not throw an exception if you attempt to create a folder that already exists.

Example of directory creation:

DirectoryInfo newDir = new DirectoryInfo(@"C:\deleteme");
if (newDir.Exists)
    Console.WriteLine("The folder already exists");
else
    newDir.Create();

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

My guess is that TempUpdateDir is a relative directory name, and it doesn't actually refer to where you think it does. It's hard to say without any more information though. It would be useful to log the absolute path as well, to make it easier to check.

Upvotes: 1

Related Questions