Solved

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

Posted on 2009-07-04
4
840 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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 discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

11 Experts available now in Live!

Get 1:1 Help Now