Solved

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

Posted on 2009-07-04
4
987 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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