SPContentTypeCollection listctc = list.ContentTypes;

In that line,. I am getting this error:

Invalid file name.

The file name you specified could not be used. It may be the name of an existing file or directory, or you may not have permission to access the file

Any idea??

protected void Page_Load(object sender, EventArgs e)
                using (SPSite NewSite = new SPSite(SPContext.Current.Web.Url))
                    SPWeb CurrentWeb = NewSite.RootWeb;
                    //using (SPWeb CurrentWeb = NewSite.RootWeb)

                        SiteHomeUrl.Text = "<a href=\"" + CurrentWeb.Url + "\">" + CurrentWeb.Title + " Home</a>";
                        SPWebCollection subSites = CurrentWeb.Webs;
                        string nos = subSites.Count.ToString();
                        if (nos != "0")
                            AllSubSites.Text = "<li><a href=\"#nogo\">Sites</a><ul>";
                            for (int i = 0; i < subSites.Count; i++)
                                using (SPWeb subSite = subSites[i])
                                    AllSubSites.Text += "<li><a href=\"" + subSite.Url + "\">" + subSite.Title + "</a></li>";
                            AllSubSites.Text += "</ul></li>";
                        else AllSubSites.Text = string.Empty;

                        foreach (SPList list in CurrentWeb.Lists)
                            SPContentTypeCollection listctc = list.ContentTypes;
                            foreach (SPContentType ct in listctc)


enter image description here

  • Debugging I could notice its in a specific contenttype that it throws the exception. Apr 4, 2012 at 13:18
  • i cant read the log screen shot maybe paste it as plain text Apr 4, 2012 at 22:59
  • did you try zoom in your browser? Apr 5, 2012 at 7:15

1 Answer 1


I think this is one of the most weird errors I ever had in SP 2010, it took me about 3 hours to locate the problem and the fix was pretty simple.

In my current environmnet, I have 3 sharepoint solutions, each solution is for different sharepoint templates we have.

In each solution, there are content types, lists, webtemplates, webparts, etc, etc.

When I started to debug the problem, I saw the error was on the following line, where it iterates over all contenttypes for each list.

foreach (SPList list in CurrentWeb.Lists)
SPContentTypeCollection listctc = list.ContentTypes;
foreach (SPContentType ct in listctc)

the line list.ContentTYpes was the one causing the issue, but why? If everything was working fine?

Well, here is the trick!!!

Because I have 3 different solutions, I made a mistake in one of them, I removed one content type from one feature, so when the solution was updated, it was working fine.

Then in the other solution, after I updadted that one, the problem came up.

The cause is that when it iterates over all SpContentTypeCollection for each list, the content type exists in SHarepoint, but the feature definition file does not exist in the feature folder.

Really weird!

The fix: I added the content type again in the 1st solution to the feature, recompiled, repackaged, updated the solution and voila. My site is working again.

This was really complcated to solve, as I could not find anywhere on the net with the exact same problem.

I have blogged this here: http://levalencia.wordpress.com/2012/04/07/the-file-name-you-specified-could-not-be-used-it-may-be-the-name-of-an-existing-file-or-directory/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.