Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-07-04
4
Medium Priority
?
1,146 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

636 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