How can I show a message when I open a File in VS 2008?
I want to 开发者_运维百科show a warning when a certain file is opened, how can this be done? for example if I open the Program.cs a warning dialog pops up: "If you edit this you are gonna die"
You have two options to extend Visual Studio: Add-ins and Visual Studio Packages.
Addins are relatively easy to implement and don't have any complex installation requirements.
Packages are more capable but require Package Load Keys, which you have to request from a Microsoft site.
It sounds like you could get by with an Addin if all you want to do is raise a dialog. Look into the IVsUIShell interface and uiShell.ShowMessageBox() method to raise dialogs through the VS Shell. Here's a tutorial on creating addins.
Several options:
Store the file version control (tell me you are using version control... please!) and keep it locked, or use your version control system to only allow certain users to make changes.
If this is .NET code, you can put it in a region with comments, etc and make it really obvious to the casual browser what you need.
Again, if this is .NET and you want to isolate this code, you can use partial classes and move just the problematic code to a completely different source file (kind of like the designer does for Forms/Controls/Resources). That way your "public" code is all in once place and your "private/problematic" code is in it's own file which potentially you can protect with version control (see above).
If you have more free time than you should, writing a custom visual studio add-on would be a good solution, but you need to seriously consider the usability issues involved with that.
You DO NOT want to simply throw up a message box any time this file is opened. That is EVIL... EVIL I tell ya! You never know how the file is going to opened. What if it's from a multi-file text search? You are suddenly going to interrupting automatic operations in the IDE with these irritating message boxes.
Rather, you need some kind of solution which is in-your-face when appropriate, but is completely out-of-your-face when not. I would suggest some kind of rendering change in the text of the IDE that is affected or perhaps a floating window (kind of like MSN Messenger notification window) that appears and perhaps automatically disappears after a timeout.
I would strongly suggest you look at DXCore from Developer Express. This is a free extensibility framework which supports VS2005/2008/2010. It has lots of ways to create tool windows, popup windows, perform animations and draw stuff in the text window, perform condition event notifications for when your cursor or display view is located in a specific piece of code. In short, it's got the foundation of what you want.
If I were writing this as an add-on, I would do something like this:- Surround the code in question with a #region with a specific text marker in the region text. Something like "#region Notify Developer -- Don't change this code. Here be dragon's. Call Simon before you touch this code!"
- Create a visual studio add-on using DXCore which looks for #region tags with this specific marker (a #region with "Notify Developer" as the lead text).
- When found, these regions would get rendered in a special way: Put a border around the region, do a semi-opaque fill to "fade" the region, place the message over the middle of the region (or the middle of the visible part of the region).
- Allow the use to dismiss the message ('x' in corner) which would result in the opaque fade being dismissed as well.
This would allow the offending code to only be in-your-face when it is actually visible, and not otherwise.
Developer Express uses their free DXCore framework to create CodeRush and Refactor Pro!, which both add lots of graphical eye-candy to source files (for example, CodeRush does change the way that regions render already). And using DXCore, you get free future-proofing for newer IDE versions. Nice.
But at some level, I have to ask, why spent this much effort when a comment or one of the other methods is good enough. This begins to smack a little bit of micro-management of your coders. If you have a developer you don't trust to read a comment and responsibly do their job, why did you hire them? Will you trust them to run your add-on? Will you trust them to do anything else? I think this is overkill. Add a comment... maybe split out the source into multiple files... definitely use version control. Don't be a jerk. Nobody likes a jerk.
精彩评论