?
Solved

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

Posted on 2009-07-04
4
Medium Priority
?
1,066 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:
Munawar Hussain earned 2000 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:Munawar Hussain
ID: 24788067
good to know you got solution :)

-thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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 …
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: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

762 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