tags:

views:

31

answers:

2

How can I programmatically add files to a TFS project that have code behind files. I can say the following to add files. That will only add single files to a project and not the file plus the code behind file. I'm trying to add a resource file and it's code behind that were dynamically generated to a TFS project.

workspace.PendAdd(filesWithPathToEdit, true);
A: 

There's no way for it to automatically know if a file depends on another. However, you can decide on your own which files will typically have a code behind file associated with them and add them yourself.

For example:

If you begin to add a file with an .aspx extension, then those files, as we know, typically have a code behind file. That code behind file, we can assume, has the same file name, with .cs appended. So, if we have "Default.aspx", then we can safely assume that there will be a "Default.aspx.cs" and that they are dependent on each other, so we should add both.

The same thing goes with .xaml and .xaml.cs files.

Ryan Hayes
I'm trying to programmatically add it. If you add both of those files using the Workspace.PendAdd there is no dependency between them. I think there is a way to do it using EnvDTE assembly and grab the project file i want to add the files too and modify it.
jspence
A: 

I had to put it in a T4 template to get access to the current Visual Studio DTE otherwise it would randomly work if I tried it outside of a t4. You can use the DTE to get a list of projects from a solution then add a ProjectItem and it contains ProjectItems so you can add your code behind there. ResxContainer is a custom class to just contain all information about my resx file i needed.

EnvDTE.DTE dte = (EnvDTE.DTE)HostServiceProvider.GetService(typeof(EnvDTE.DTE));
            //dte = (EnvDTE.DTE) hostServiceProvider.GetService(typeof(EnvDTE.DTE));
            //dte = (EnvDTE80.DTE2)Marshal.GetActiveObject("VisualStudio.DTE");
            Projects projects = dte.Solution.Projects;
            if (projects.Count > 0)
            {
                IEnumerator enumer = ((IEnumerable)projects).GetEnumerator();
                while (enumer.MoveNext())
                {
                    Project proj = (Project)enumer.Current;
                    if (proj.Name == projectName)
                    {
                        foreach (ResxContainer res in items)
                        {
                            ProjectItem item = proj.ProjectItems.AddFromFile(res.ResxPath);
                            item.ProjectItems.AddFromFile(res.CodeBehindPath);
                        }
                    }
                }
jspence