Changing Project Metadata in Visual Studio 2013 C# - c#

I've got a Visual Studio 2013 package written in C# where I need to make some changes in the items and their attributes in a C++ project file while Visual Studio 2013 is running and has the project loaded. Actually, it could also be OK to save the project file at the point when the whole solution is being saved.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="Test">
<MyItemToChange Include = "TestItem">
<MyMetadata1ToChange>Value1</MyMetadata1ToChange>
<MyMetadata2ToChange>Value2</MyMetadata2ToChange>
</MyItemToChange>
</ItemGroup>
<!-- The rest of the file -->
</Project>
In previous Visual Studio versions I used the Microsoft.Build.Evaluation.Project class to change the items and the metadata of the project. When you have the reference to the project, you can either accessed the XML structure of the project directly through Project.Xml property or get a hold on different Microsoft.Build.Evaluation.ProjectItem and use the interfaces found there.
However, since Microsoft has changed the structure of Visual Studio in their 2013 release, though the Microsoft.Build.Evaluation.Project is not deprecated, you can't access the C++ projects any more through Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.LoadedProjects as the collection is empty. However it still works if you want to access C# projects. (As a matter of fact, browsing deep into the objects while debugging you can find that they still use the same Microsoft.Build.Evaluation namespace for inner representation.)
Since I can't access the Microsoft.Build.Evaluation.Project any more, I need an alternative to
access the project otherwise than using the
Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.LoadedProjects
write project items and metadata while Visual Studio 2013 is running.
Unless I've missed something, the following are not good for my purposes:
Microsoft.VisualStudio.Shell.Interop.IVsBuildPropertyStorage.SetItemAttribute sets only attributes to items that are displayed in the Solution Explorer. "MyItemToChange" is not displayed in the Solution Explorer
Opening the file and manually editing and saving it. Unless you know how to suppress Visual Studio's warning about a changed project and how to auto reload it, it is not a way to go.
Note: You can close the solution, open the project file manually, edit then save it and reopen the solution. It would work fine, the data is there, unless the files are under source control. Now it opens up a whole lot of new issues (why EnvDTE.DTE.SourceControl.CheckOutItem() doesn't work, etc.) So I still think I would be better off if Visual Studio would handle writing data to the project file.
Reflection. Yes, as I mentioned before, I could dig into internal classes to get a hold on the "original" Microsoft.Build.Evaluation.Project, and Microsoft.Build.Evaluation.ProjectItem, but first of all, it is quite unsafe for future uses and as far as I remember, you cannot be sure that if you modify these now internal classes, the project will know that it is dirty and has to be saved.
I'm glad to have any suggestions as I'm really running out of options.
Thanks.

Well, I have worked out a workaround, not a real solution for the problem.
What I do right now is the following:
Ask the user whether they're OK with closing the whole solution and saving everything. If not, I cancel the whole procedure: EnvDTE.DTE.ItemOperations.PromptToSave
Try to ask Visual Studio 2013 to check out the project file for me in source control using EnvDTE.DTE.SourceControl.CheckOutItem() method. If it throws an exception (NotImplementedException), I use a bodge to check out the project file. That is, I write a custom property into the project file using Microsoft.VisualStudio.Shell.Interop.IVsBuildPropertyStorage.SetProperty() method.
Close the whole solution. EnvDTE.DTE.Solution.Close()
Open the project file using the System.Xml.XmlDocument class. Edit. Save. Oh, and remove my silly property written in the 2. point (the one used to force Visual Studio 2013 to check out the file in source control.)
Reopen the solution. EnvDTE.DTE.Solution.Open() At this point the user might be asked again to confirm some source control options.
Why is it just a workaround not a proper solution?
It's ugly. :)
Methods are abused for things they are not meant to be used (writing a random property into a project just to force source control checkout)
It wouldn't work if closing the solution / project is not an option. E.g.: if you need to save something during build or while they are editing the solution / project / source files; or if you have to do it often not just once during the lifetime of a project.
I'm still looking for the proper solution for the problem. But until someone could tell me how it is done, I have to live with this current implementation.

Related

Visual Studio Intellisense not showing some classes

My Visual Studio is not showing a lot of the available classes in the IntelliSense auto-complete. For example, a project I'm working on has a reference to the Microsoft.Xna.Framework namespace, yet even after adding the using statement using Microsoft.Xna.Framework in a file, IntelliSense is unable to recognize classes like Texture2D and Rectangle. I can still type them in, however, without getting a compiler error. Any idea what's happening?
The IntelliSense detects standard libraries like System.Diagnostics but not any external ones.
It is not a NameSpace conflict issue because classes within the current namespace are also missing from the IntelliSense.
I have tried:
Restarting Visual Studio (With and without closing tabs)
Deleting the contents of %LocalAppData%\Microsoft\VisualStudio\14.0\ComponentCacheModel folder
Resetting import and export settings
What worked for me is unloading and reloading the project
If unloading/reloading the project doesn't work, try deleting the .suo file found in the SolutionName/.vs/ folder. The .vs folder is an auto-created, hidden folder so you will have to show hidden folders in order to see it.
I am using ReSharper. What worked for me:
Unload project.
ReSharper > Options > Environment > General > Clear caches
Close solution.
Exit Visual Studio.
Restart Visual Studio.
Reload project.
Do you have ReSharper or any other plugins installed? There sounds like a similar issue/solution here. It may be third-party plugins at play depending on what add-ons you have.
Try saving and reloading the files.
I don't know why it happens, but it happens to my work laptop occasionally, typically reopening the source file resolves it.
OK so probably not the best solution but it works. I reinstalled Visual Studio and the problem was fixed. Also, it hasn't shown up since (this happened about 3 months ago).
another dumb solution))) Determine which classes/namespaces are not showing up. Then go to that project where they are. In that project pick any .cs file copy its contents, then delete that file and create again))
Click on the class not showing and Change Build Action -> to Compile
it happens sometimes when you add class as content or other resources

Visual Studio 2013 only builds HTML changes, not C# changes

I am building a personal website using asp.NET's webforms in visual studio 2013 express for web and am following this tutorial:
http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/introduction-and-overview
My page is structured exactly the same as in the website, I have changed some minor stuff to make it my own but the structure in terms of the C# classes and how the interact with the HTML are exactly the same.
I got to section 5 of the tutorial "Display Data Items and Details" and everything was working fine. I've used git a lot in the past so I decided to create a repository for this project so I can access it at work if I feel like.
Suddenly now when I make changes to the C# classes it won't build. It's even stranger because I if I make a change on an HTML file the change is built. In section 3 of the tutorial we learnt how to make the 'product' classes which are displayed on the products page. If I want to change one of the product names for example, when I build the change is not there. Simultaneously I went and changed some info in the HTML for the contact page, IT CHANGES when I build. Why wont the C# changes take effect when I build any more?
I am relatively new to both asp.NET and visual studio. The HTML changes when I build and the C# does not. When I change either I can see in solution explorer that there is a red tick for pending changes. Why would only the HTML pending change be included in the build and not the C#? How do I ensure that the build is actually building the version I see in my editor window?
EDIT
I do not know if I found the original cause but I found a solution/workaround. I realized that the classes mentioned above were grabbed by the html page from the page's database. The .mdf file for the solution was not being rebuilt whenever I cleaned and built so I physically deleted it and rebuilt the solution and voilla my C# changes occurred. I am still fairly new to this whole thing, can someone explain what the .mdf file does and why it wasn't being rebuilt?
Check your .cs files properties on the properties window in visual studio to make sure their Build Action is set to "Compile", Things that are not set to "Compile" do not get compiled. How MSBuild treats project items depends entirely on their build action. CS files default to "Compile" when you make them, but if you changed them yourself that would be why it doesn't update. Also CS files placed in the App_Start folder default to "Content" and they are compiled by ASP.Net when the Application Starts, so if you changed something in App_Start you need to reset the site.
Not exactly sure what is the problem, but I would do a right mouse click on the solution in Solution Explorer->Clean Solution, then do another build and see if that helps.

Check In: Operation not performed Could not find file *.csproj.vspscc

I am having issues with check in my code files because of some changes I have made to the project and solution. I have renamed project files, added different project files in the solution and added many files in the existing project.
Now I am getting an error while checking in the code.
The error details are:
Check In: Operation not performed Could not find file
'....Console.csproj.vspscc'.
How do I create a vspscc file if it does not get created on its own?
Any help would be great and thanks in advance.
This looks pretty stupid on my part. I restarted my Visual Studio and I could perform the check in because Visual Studio created the vspscc file when it opened the projects and solution.
Answering my own question as fast as possible so nobody else prints my own negligence before me.
And for anybody who needs an answer as how to create a vspscc file, restart the visual studio IDE
I have also seen this in another situation (VS2012) where a simple restart didn't do the trick.
I had a number of new projects which I had added to an existing solution but in the wrong place. Undoing and re-adding seemed to be fine until check-in, where it complained that the newly added projects and related .vspscc files could not be found. It was looking for them in the original location rather than where they are now.
Resolution was to go to the Source Control Explorer, which still had check-in items against these files in their original (but non-existing) locations, as well as the files in their correct locations. Selecting these errant references and "Undo pending Changes" for these files did resolve the issue.
I'd try using File/SourceControl/ChangeSourceControl dialog and unbind (if necessary) then rebind the project to TFS to recreate the .csproj.vspscc.
I removed projects and have same issue, so my solution was to re-create the same projects with same name, then remove them cleanly :-) I hope this helps
I had a similar problem, but in my case I Visual Studio/TFS couldn't fine the .csproj file. Anyway, with the help of a colleague I was able to determine that the issue was within TFS itself. TFS may have a different view of your source code than Visual Studio does. I had created the wrong type of project (I'll call it Proj1) and deleted it from within VS. Then created the correct project type, which I'll call Proj2. When I when to check it into TFS it complained about Prog1.csproj, which confused me because I couldn't see it within VS. What I learned was that you have to go into TFS's Source Control Explorer. From there you'll see how TFS sees things. Sure enough, there was Proj1.csproj (and some other related files to Proj1). What I had to do within Source Control Explorer was undo the pending check-in's of those files and folders which I didn't want to check in and no longer existed anyway. Then TFS was happy and I could perform the check-in.

Add-in for Visual Studio for project file editing (csproj)

Do you know an add-in for visual studio which adds properties for C# projects?
csproj file format is very powerful but only small amount of options can be changed through the standard properties page. For instance, I want to have several configurations and each of them should include it's own references. Or I just want to change the type of project from winforms to wpf or other one. In order to do stuff like that developer has to edit csproj by hands in text editor instead of using properties.
If you know that such add-in doesn't exist, do you think that it could be popular on codeplex? or only small amount of developers realy need it? what is your opinion?
How is it going?
To edit project files inside of Visual Studio I use PowerCommands, but those things you can change "using properties" are those in Project Menu -> [Project Name] Properties. What PowerCommands does (among other things...) is allow you to easily edit the XML of a project opened inside Visual Studio so you get all the benefits of using Visual Studio to edit XML. Makes sense?
XML is hierarchical, so hardly you can fit it comfortably on a property list better than on a text editor.
About "I want to have several configurations and each of them should include it's own references", try creating templates for your projects, take a look there: Visual Studio Templates.
So... you are looking for addins at codeplex? why not take a look at http://visualstudiogallery.msdn.microsoft.com there are free and paid addins, an some form codeplex too. While you are on it, look for "Productivity Power Tools", "VSCommands 2010", "AllMargins" and "VS10x Code Map". I use them all, and they are certainly worth a look. [Hmmm... I also use CodeRush, you can get CodeRush Xpress for free from http://www.devexpress.com ;)]
Hope of being of help.
A little late to the table but I wanted to remove VSCommands 2010 because all I use it for really is the edit project file and I don't like to just have extensions hanging around if I am not using them. I discovered from this blog:
http://blogs.msdn.com/b/habibh/archive/2009/07/01/the-quickest-way-to-edit-a-visual-studio-project-file.aspx
"There is a quick and easy way of editing your Visual Studio project file. When a project is "unloaded", either because you explicitly chose to unload the project using the "Unload Project" command or Visual Studio failed to load the project for some reason (e.g. project upgrade failed), you can right click on the project in Solution Explorer and select the "Edit " command, as shown below."
It works like a charm!

Any way to make Intellisense work, when opening a cs file that's not part of the project?

What we ideally need is, to know how Microsoft handles XAML generated code (Those *.g.cs files). If you goto a XAML code behind, intellisense will work even if the *.g.cs file is not part of the project!!
Context:
In a custom VS package, we need to have some logic to open a CS file (with out making it a part of the project) in the VS IDE, to allow the user to edit it.
We are hooking up the document to the Running document table and receiving the events like Saving, Close and all, using IVSRunningDocumentTable. Works good.
Now the problem is, when the document is opened, Intellisense can't work, for the simple reason that the opened document is not part of the project (sadly, we can't do that, we can't make it code behind).
Intellisense is driven by a memory cache of identifiers and types. These types are cached based on the project you are in and the references that project has. If the code file you are editing is not part of a project, Visual Studio would have to load every possible assembly and create intellisense data for each type in the entire .NET framework because it would have no way of knowing whether or not your code file required it.
I guess Visual Studio could load intellisense based on the content of the file but that is not the way it currently works.
Visual Assist X by Whole Tomato is an addin to VS I've been using for many years. It will give you Intellisense and more when you open it.

Categories