locked
adding a file to a folder fails RRS feed

  • Question

  • Hi,

    I have a wizard that adds my own C# file to a project. If I'm adding to the root folder of the project e.g. ConsoleApplication1 then its fine. If I create a folder in Visual Studio e.g. Folder1 then use my wizard I get "Specified cast is not valid".

    I get the namespace name from the projectitems.Parent object which I cast to an EnvDTE.Project.

    The problem is because, when adding a file to a folder, the projectitems.Parent is not a EnvDTE.Project object.

    So what is it? Is there a way of working it out?

    Jason
    Thursday, December 1, 2005 2:50 PM

Answers

  • ProjectItems.Parent can return either a Project if the item is located directly underneath the project node, or it can return another ProjectItems collection if the node is within another collection of items.

    Namespaces are computed by calling Project.Properties.Item("RootNamespace").Value, then appending the folder hierarchy (seperated by periods) for VB, C#, or J# projects. But it appears that you are using a web project, and there is no property for retrieving the namespace. In fact, there is no real namespace hierarchy for web projects.

    Craig
    Monday, December 5, 2005 7:14 PM

All replies

  • Project is the root of the project. all items within it are ProjectItem instances.
    Thursday, December 1, 2005 3:15 PM
  • ok.

    I'm trying to work out the namespace of the C# file before I add it. I think this comes from the DefaultNamespace property. As projectitems does not support properties, I'm using the Project object (which just happens to work when the class is added to the root).

    So my question is now- how do i work out the namespace of my c# file before i add it? The only object I seem to have to work with is the ProjectItems collection (which comes from ContextParams[2])

    Jason

    Thursday, December 1, 2005 3:23 PM
  • here's what i did. Document can be an ascx file in my case, Item is a project item i just created that i'm going to generate code into. this method looks for any namespaces in the given file and call some other processing method when it finds one.



    public virtual void processFile(Document Document, ProjectItem Item)
    {
        FileCodeModel fcm = Document.ProjectItem.FileCodeModel;

        if (fcm == null)
            return;

        ProjectItem projectItem = Item;
        if (projectItem.Document == null)
            projectItem.Open(Constants.vsViewKindCode);

        FileCodeModel targetModel = projectItem.FileCodeModel;

        resetModel(targetModel);

        if (fcm.CodeElements.Count == 0)
            fcm.Parent.Open(Constants.vsViewKindCode);

        if (Document.Name.EndsWith(".ascx"))
            Document.ProjectItem.Collection.Item(Document.Name + ".cs").Open(Constants.vsViewKindCode).Activate();

        foreach (CodeElement element in fcm.CodeElements)
        {
            if (element.Kind == vsCMElement.vsCMElementNamespace)
                processNamespace((CodeNamespace)element, targetModel);
        }

        projectItem.Save("");
    }

     


     

    Thursday, December 1, 2005 5:23 PM
  • wait... i think i misread what you're asking... you're adding a new file to a folder and want to know what namespace that defaults the file to? in that case do projectItem.get_FileNames(0) (or 1? their indeces are all over the place) to get the full file name of the folder and then figure out what the relative path is to the project itself (projectItem.ContainingProject.FileName) and then use that in conjunction with the default namespace on projectItem.ContainingProject.
    Thursday, December 1, 2005 5:27 PM
  • that looks hard :-(

    you're right - I want to copy the functionality that the standard wizard uses to replace SAFE_NAMESPACE_NAME with the namespace of the files. The wizard gets the namespace from the "DefaultNamespace" property of the Parent object of the projectitems collection.

    However, I can't do this if I don't know what type the parent is!! It is not always a EnvDTE.Project and its property is just defined as object.

    So my question should be- what different types can the parent property of a projectitems collection be, and how can I tell? I may submit this as a separate question.

    I'm writing my own wizard and so cannot use the standard one.

    Thanks for your help to date
    Jason
    Friday, December 2, 2005 12:43 PM
  • ProjectItems.Parent can return either a Project if the item is located directly underneath the project node, or it can return another ProjectItems collection if the node is within another collection of items.

    Namespaces are computed by calling Project.Properties.Item("RootNamespace").Value, then appending the folder hierarchy (seperated by periods) for VB, C#, or J# projects. But it appears that you are using a web project, and there is no property for retrieving the namespace. In fact, there is no real namespace hierarchy for web projects.

    Craig
    Monday, December 5, 2005 7:14 PM