Solved

Cannot create <path> because a file or directory with the same name already exists.

Posted on 2009-07-04
4
863 Views
Last Modified: 2013-12-17
Hi,

I have a ASP.Net app on my dev laptop XP Pro SP3. I am trying to create a new directory as a child directory of my web app.

When I try to create it I get an error: "Cannot create <path> because a file or directory with the same name already exists.". <path> is the path to the new directory.

In my code I first check to see if the directoy exists and if not then I try to create it (see code below).

I have added ASPNet to the directory security and given it full permission.

Any one have an idea as why I'm not able to create a directory?
if (! Directory.Exists(Path.GetDirectoryName(saveToPath)) )

                {

                    string saveDir = Path.GetDirectoryName(saveToPath);

 

                    DirectoryInfo objDirectoryInfo = Directory.CreateDirectory(saveDir);

                }

Open in new window

0
Comment
Question by:Asparona
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
needo_jee earned 500 total points
ID: 24776740
hi

make sure that your directory path ends with a back slash
for example the
saveToPath  should be


To create a directory "test3" your path should look like this...
saveToPath  = "C:\test1\Test2\Test3\

IT would not work if you pass a path like this

saveToPath  = "C:\test1\Test2\Test3

because for above line  Path.GetDirectoryName(saveToPath) will return "C:\test1\Test2" and the Test2 already exists.


-thanks
0
 

Author Comment

by:Asparona
ID: 24777878
It was missing the final slash and I changed my code to include it (see snippet) however I get the same error message.

I have now given everyone full permission in the directory security with no change in the error message.
                if (! Directory.Exists(Path.GetDirectoryName(saveToPath)) )

                {

                    saveDir = Path.GetDirectoryName(saveToPath);
 

                    if (! saveDir.EndsWith("\\"))

                    {

                        saveDir += "\\";

                    }
 

                    DirectoryInfo objDirectoryInfo = Directory.CreateDirectory(saveDir);

                }

Open in new window

0
 

Author Comment

by:Asparona
ID: 24777941
Doh!

I found an errant file in my directory with the same name as the directory I was trying to create, remnants of me playing around with the FileStream earlier to generate a file and not having the complete file path at the time.

I didn't realize it actually created the empty file and gave it the directory name and it was hard to spot amongst the other files.

Thanks for the tip of ending the directory name with a slash!
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 24788067
good to know you got solution :)

-thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now