Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 17th, 2012, 06:58 PM
Authorized User
 
Join Date: Jul 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default Directory.Exists gives false positives, and Directory.CreateDirectory fails to create

EDIT: Please ignore (or delete) this post. I am, without doubt, an idiot. I was looking in C:\Users\me\Roaming and not in C:\Users\me\AppData\Roaming. And to think they trust me behind a keyboard...


Below is a class I'm working on. The constructor looks to see if a directory exists and if it doesn't it creates it. Well that's the theory anyway.

Here's the code (problem described below):

Code:
class UserManager {

        string app_data = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString();
        string app_dir, config_file;
        string working_dir;

        public UserManager() {

            app_dir = app_data + @"\Foo2";                       // Where we store user data
            config_file = app_dir + @"\userdata.config";        // User configuration file


            // Read the configuration file if it exists...
            if (File.Exists(config_file)) {
                loadUserData();
            
            // ...if there is no configuration file then lets create one.
            } else {
                
                // Most likely the correct directory won't exist, so we make it.
                if  ( ! Directory.Exists(app_dir) ) {
                    try {
                        Directory.CreateDirectory(app_dir);
                    } catch {
                        MessageBox.Show("Error creating directory, " + app_dir);
                    }
                }

            }
            
        }

        public void loadUserData () {
            
        }

    }
OK, so I've tried single-stepping through this to find out what's wrong and I found two issues that don't act the way I would expect them to.

First: The if statement, if ( ! Directory.Exists(app_dir) ), shows true, indicating that the directory does not exist, the first time I run it. When I run it a second time it shows false as if the directory now exists however I do not see the directory via Windows Explorer.

If I change the name from "Foo" to "Foo1" then it, again, works the first time and fails from then again. Changing to "Foo2" repeats the cycle, and so on.

Second: when the if statement is true (directory doesn't exist) then the Directory.CreateDirectory(app_dir) executes without error but, once again, no directory is actually created.

As always, thanks for any and all assistance. :)

Last edited by KaneT; July 17th, 2012 at 07:42 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Check to make sure a directory exists stonesbg ASP.NET 2.0 Basics 3 January 18th, 2007 03:25 PM
How to Create new folder(directory) in Srever ? Raam ASP.NET 2.0 Professional 1 May 10th, 2006 12:48 PM
create xsl file in c directory keyvanjan Classic ASP Basics 2 June 21st, 2005 04:52 PM
check for directory, or create new if not found amerk20 VB How-To 1 April 8th, 2005 12:18 PM
could not create new directory in browserfile.aspx raja_699 BOOK: ASP.NET Website Programming Problem-Design-Solution 2 January 25th, 2005 01:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.