共用方式為


Using the tf.exe command-line as a quick-and-dirty way of complementing the VS experience

One of the things I find myself wanting to do fairly often while working in VS is to see the changes I've made in the current file since pending the edit. VS itself has support for this during the current editing session, but sometimes it's been weeks since I made a change, or I just unshelved it, or whatnot.  What I commonly did was right-click on the tab for the file in VS and selected "Copy Full Path" and then went to a cmd.exe window and typed "tf diff " and then right-clicked in the window to paste (QuickEdit mode, IMHO, should really be on by default), and then hit enter. [Edit] Another alternative is to use (File | Source Control | Compare), perhaps via Alt+F,R,C to bring up the Compare dialog, which by default will also show the pending edits, but that requires another click or hitting Enter.

Since I caught myself doing that often enough, I made a VS add-in called "DiffAddin" (File | New | Project | Other Project Types | Extensibility | Visual Studio Add-in) and had it do that work for me.  Note that we have an object model and that's the better route for this, but this is meant to show that you can easily let the tf.exe command help augment your in-UI experience with VS.

Once I went through the wizard, I hopped over to the Exec method in DiffAddin.Connect and inserted a ShowDiff(); call before handled = true; and then added this simple method:

    private void ShowDiff()

    {

        ProcessStartInfo psi = new ProcessStartInfo();

        psi.FileName = "tf.exe";

        psi.Arguments = String.Format("diff {0}", _applicationObject.ActiveDocument.FullName);

        psi.CreateNoWindow = true; // we don't want a pesky window

        psi.UseShellExecute = false; // don't bother with ShellExecute

        // Don't bother to wait for it to exit, we don't care

        System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi);

    }

I hit F5 to test it (which runs VS with the necessary flags - you can deploy via the .addin file going into My Documents\Visual Studio 2005\Addins) by opening a TF-controlled solution and using Tools | DiffAddin to confirm it correctly brings up my configured diff tool (by default, diffmerge.exe).  Since that's working, I then bind it to ctrl+shift+d (Tools | Options | Environment | Keyboard, binding DiffAddin.Connect.DiffAddin) and confirm that works (to bind to the File | SourceControl | Compare behavior, you can bind the File.TfsCompare command).  Now I can hit ctrl-shift-d to invoke my addin and do a quick pending-edit diff of the current document whenever I want.

It doesn't do any error checking or other things you'd likely want to do with a real add-in, but hopefully this shows that you can quickly leverage tf.exe (not to mention, of course, our object model) for augmenting your experience.  It's not a great example since File.TfsCompare is a better thing to bind (and it's already available), but hopefully it serves as an example nonetheless.