开发者

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("Downl开发者_如何学编程oader", 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....?????????????


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.


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜