Archive for the ‘Visual Studio and tools’ Category
Posted by jpluimers on 2012/01/26
This is WinForms code from a long time ago, but the concept of using an IDisposable interface to do resource cleanup and restore a temporary setting is very valid.
You use the code below like this:
private void myMethod()
{
// set busy cursor
using (IDisposable waitCursor = new TemporaryCursor(this, System.Windows.Forms.Cursors.WaitCursor))
{
// logic that takes a long while
}
}
The code below implements the TemporaryCursor class; you can assign any System.Windows.Forms.Cursors item you want.
It restores the cursor upon these three “events”:
Most often the IDispose pattern is being used to make sure that resources get cleaned up. If you think of a wait cursor as a temporary resource, this example becomes much easier to remember.
Of course this is not limited to the System.Windows.Forms realm, you can just as well use this for non-visual temporaries, and other kinds of UIs like ASP.NET, WPF or SilverLight.
using System.Windows.Forms;
namespace bo.Windows.Forms
{
public class TemporaryCursor : IDisposable
{
private Control targetControl;
private Cursor savedCursor;
private Cursor temporaryCursor;
private bool disposed = false;
public TemporaryCursor(Control targetControl, Cursor temporaryCursor)
{
if (null == targetControl)
throw new ArgumentNullException("targetControl");
if (null == temporaryCursor)
throw new ArgumentNullException("temporaryCursor");
this.targetControl = targetControl;
this.temporaryCursor = temporaryCursor;
savedCursor = targetControl.Cursor;
targetControl.Cursor = temporaryCursor;
targetControl.HandleDestroyed += new EventHandler(targetControl_HandleDestroyed);
}
void targetControl_HandleDestroyed(object sender, EventArgs e)
{
if (null != targetControl)
if (!targetControl.RecreatingHandle)
targetControl = null;
}
// public so you can call it on the class instance as well as through IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (null != targetControl)
{
targetControl.HandleDestroyed -= new EventHandler(targetControl_HandleDestroyed);
if (temporaryCursor == targetControl.Cursor)
targetControl.Cursor = savedCursor;
targetControl = null;
}
disposed = true;
}
}
// Finalizer
~TemporaryCursor()
{
Dispose(false);
}
}
}
–jeroen
Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Development, Software Development, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools, WinForms | 2 Comments »
Posted by jpluimers on 2012/01/24
Every once in a while you need to maintain really old stuff, and start update an old VM.
In case of Visual Studio 2005, the Windows Update and Microsoft Update will get you into a condition where it cannot install ”Security Update for Microsoft Visual Studio 2005 Service Pack 1 XML Editor (KB2251481)“. Not even the direct download will install.
The search for ”some updates were not installed” “Security Update for Microsoft Visual Studio 2005 Service Pack 1 XML Editor (KB2251481)” pointed me to the solution:
There are two versions of KB2251481 June and August. When the June version is installed, the August version refuses to install.
Uninstall the original KB2251481 from the Control Panel. Then reinstall the August version.
The KB2251481 article mentions this only for the “Microsoft Visual Studio 2005 Premier Partner Edition SP1″, but it happens with other Visual Studio 2005 editions as well.
–jeroen
via: KB2251481 Security Update for Microsoft Visual Studio 2005 Service – Microsoft Answers.
Posted in .NET, Development, Software Development, Visual Studio 2005, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/12/29
In addition to the ASP.NET hash collision Denial of Service attack, Microsoft patches 3 more vulnerabilities resulting in an Aggregate Severity Rating that is Critical.
This is a summary of the vulnerabilities. Please read the full MS11-100 bulletin for more details and how to download and install the patches.
| Vulnerability Severity Rating |
Maximum Security Impact |
Affected Software |
CVE ID |
| Important |
Denial of Service |
Collisions in HashTable May Cause DoS Vulnerability |
CVE-2011-3414 |
| N/A or Moderate |
N/A or Spoofing |
Insecure Redirect in .NET Form Authentication Vulnerability |
CVE-2011-3415 |
| Critical |
Elevation of Privilege |
ASP.Net Forms Authentication Bypass Vulnerability |
CVE-2011-3416 |
| Important |
Elevation of Privilege |
ASP.NET Forms Authentication Ticket Caching Vulnerability |
CVE-2011-3417 |
The CVE-2011-3415 is N/A in .NET 1.1, and Moderate in all other .NET versions.
–jeroen
via Microsoft Security Bulletin MS11-100 – Critical : Vulnerabilities in .NET Framework Could Allow Elevation of Privilege (2638420).
Posted in .NET, ASP.NET, C#, Development, Software Development, VB.NET, Visual Studio and tools | Tagged: denial of service attack, dos vulnerability, hash collision, microsoft patches, microsoft security bulletin, severity rating | Leave a Comment »
Posted by jpluimers on 2011/12/28
While re-designing a Visual Studio 2010 plus Delphi XE2 install for a specific client, I updated some of my Tools page links:
–jeroen
Posted in .NET, C#, Delphi, Development, Software Development, TFS (Team Foundation System), Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/12/13
When doing WinForms development in Visual Studio 2010 (including SP1), be aware of a bug with UserControls that hamper debugging; sometimes you get an error like this:
Error 9 Unable to copy file "obj\x86\Debug\MyProject.exe" to "bin\Debug\MyProject.exe". The process cannot access the file 'bin\Debug\MyProject.exe' because it is being used by another process.
When using SysInternals’ Process Explorer to see which process has handles open to MyProject, you will see that devenv.exe (The Visual Studio IDE) is the culprit: sometimes it has a lot of handles open.
The workaround is simple: close all UserControls before debugging your WinForms application.
A real pity, as UserControls are a very useful feature when developing software (many platforms use the same paradigm, .NET certainly wasn’t the first to introduce it, and it is available in for instance WPF as well).
Note that there are other causes of the same error message, but for me this was the issue.
–jeroen
Via: visual studio 2010 – VisualStudio2010 Debugging – The process cannot access the file … because it is being used by another process – Stack Overflow.
Posted in .NET, C#, Development, Software Development, VB.NET, Visual Studio 2010, Visual Studio and tools | 2 Comments »
Posted by jpluimers on 2011/09/08
While cleaning up my system, I found a while bunch of .iTrace files in “C:\Documents and Settings\All Users\Application Data\Microsoft Visual Studio\10.0\TraceDebugging” on a Windows XP system at a client that yet has to upgrade to newer Windows versions that store them under “C:\Users\All Users\Microsoft Visual Studio\10.0\TraceDebugging”.
Contrary to what IntelliTrace iTrace files and IntelliTrace Log ( .iTrace ) files and Visual Studio 2010 SP 1– Some Hidden Stuff « Abhijit’s World of .NET explains, these files are not always automatically removed.
And they are big, since Visual Studio 2010 Ultimate will automatically generate them.
So it is important to once in a while cleanup the “C:\Documents and Settings\All Users\Application Data\Microsoft Visual Studio\10.0\TraceDebugging” directory manually.
–jeroen
Posted in .NET, Development, Software Development, Visual Studio 2010, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/05/26
When you are using Team Foundation System (TFS) for version control, the project manager sometimes shows a file as being checked out by someone else, but it doesn’t show who that someone else is.
The reason is that the Project Manager only has generic knowledge about version control systems. However, the Source Control Explorer has specific knowledge about TFS.
So when you look in the Properties Window for the path of the file you are interested in, then you can use the Source Control Explorer to locate the file, and find out who has checked out that file.
There are other tools that can even give your more information than the Source Control Explorer:
- the TF command-line application (on your PATH when you start the Visual Studio Command Prompt shortcut) to obtain extra information.
- the Team Foundation Sidekicks (free; version 3.0 is for Team Foundation Server 2010; 2.4 is for Team Foundation Server 2008/2005) even produce most of that info from a GUI.
These two Stack Overflow questions were relevant in answering the above:
Posted in .NET, Development, Software Development, Source Code Management, TFS (Team Foundation System), Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/04/14
Setting up remote debugging is always a precarious thing, no matter what kind of development platform: the online documentation tells you the standard steps, but usually they don’t suffice.
This case is Visual Studio 2010 remote debugging, where the development environment is on a workstation running Windows 7, and the debug target is on Windows Server 2008 R2.
Both are x64 versions.
There is a remote desktop connection to the server, and the server can see the workstation files on the \\TSCLIENT\C share.
This is the error when running msvsmon.exe from \\tsclient\C\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Remote Debugger\x64:

[Visual Studio Remote Debugging Monitor]
The following error occurred: Not enough storage is available to complete this operation.
View Msvsmon's help for more information.
[OK]
(Funny BTW that the x64 Remote Debugging Monitor is in fact in an x86 path).
The solution is simple: copy the x64 directory local, then start it from there.
The reason in that the user credentials on the server don’t have enough rights on the \\TSCLIENT\C directory tree, so Windows barfs on it.
This pointed me into the right direction when I started Process Monitor from the same \\TSCLIENT\C share: Read the rest of this entry »
Posted in .NET, Debugging, Development, Remote Debugging, Software Development, Visual Studio 2010, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/04/07
When you have lots of projects in a solution, quickly the tree in the Solution Explorer gets a mess, especially if you have “Track Active Item in Solution Explorer” enabled.
“Solution Explorer Tools” addin for Visual Studio 2010 helps with that.
It adds three buttons to solution explorer
- Select current item
- Collapse all
- Collapse all except current item
The first one basically means you will never need to enable “Track Active Item in the Solution Explorer” again.
The second and third quickly get rid of the mess in the Solution Explorer.
Highly recommended!
–jeroen
via: Solution Explorer Tools.
Posted in .NET, Development, Software Development, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/03/30
The Download details: Microsoft Visual Studio 2010 Service Pack 1 (Installer) initially lets you download a bootstrap installer that only downloads the Service Pack 1 parts you actually need.
A bit further down on that page is the link to the full ISO DVD image with the anything you might need when updating multiple systems.
–jeroen
Posted in .NET, Development, Software Development, Visual Studio 2010, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/03/22
It seems that all versions of Visual Studio suffer from this behaviour:
Visual Studio (VS) doesn’t always clear the “Error List” window of existing errors when you compile a different project but simply appends new errors to it.
Simplest solution: Build | Clean All.
That takes some time (especially on large solutions), but I haven’t found an easier way.
Have you?
–jeroen
via: How to clear the “ErrorList” window before compiling.
Posted in .NET, Development, Software Development, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/03/17
I maintain some .NET code that still uses the MDX 1.1 (since MDX 2.0 got cancelled, and this project cannot be brought to XNA).
Every now and then, you get a Loader Lock error.
ZBfufer provides the solution (I always use choice #3): Read the rest of this entry »
Posted in .NET, C#, C# 2.0, C# 3.0, Development, Software Development, Visual Studio 2005, Visual Studio 2008, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/03/16
When you want to enable (or disable) a toolbar in Visual Studio 2010, there are three options to go, of which the last two are equivalent:
- Right click in the toolbar area to get a Context Menu, then check/unckeck the toolbar
- Right click in the toolbar area to get a Context Menu, choose Customize and check/uncheck the toolbar(s) in the dialog.
- In the Tools Menu, choose Customize and check/uncheck the toolbar(s) in the dialog.
The first one is the easiest; you can see the resulting Context Menu in the left picture (click on it to enlarge).

The last two require an extra step; you can see the resulting dialog in the right most picture (click on it to enlarge).
Given the size of those lists, you’d think all toolbars are in both.
Wrong!
These are missing from the Context Menu:
I consider this a serious Ux problem; if the Context Menu was much shorter (like 10 entries or so), it would be pretty obvious they are not.
It took me more than 10 minutes to find the Recorder toolbar which would have been vastly shorter if both lists were the same.
(Another Ux failure that caused my search to be this long is that I was looking for ‘Macro Recorder’ since all entries in the menu contain the word ‘Macro’; Recorder could as well point to a Toolbar for screen, video or audio recording).
–jeroen
Posted in .NET, Development, Software Development, Usability, User Experience, Visual Studio 2010, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/03/16
Every now and then, while recording a TemporaryMacro in Visual Studio 2010, I get this error message:
---------------------------
Microsoft Visual Studio
---------------------------
No TemporaryMacro in designated Recording Project
---------------------------
OK
---------------------------
The Recording a macro topic on MSDN suggest this solution:
Sounds like your macros directory is messed up. Open Macro Explorer (View\Other Windows\Macro Explorer) and make sure you get the MyMacros element.
However, that works.
And if I check that, the next time I record a TemporaryMacro it just works.
I never had this in Visual Studio 2008, 2005 or older.
Anyone seen this behaviour too?
Anyone who knows why this happens?
–jeroen
via: Recording a macro.
Posted in .NET, Development, Software Development, Visual Studio 2010, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/02/22
Often you work with projects not having the latest stuff.
Sometimes that is a good thing: latest stuff is not always best
In this case, the client had Office 2003, and needed to do some Excel automation from .NET.
The development systems however had Office 2007 on it, so importing Excel defaults to the Office 2007 Primary Interop Assembly: Office version 12 in stead of 11. Read the rest of this entry »
Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Delphi, Development, Prism, Software Development, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2011/02/15
If you use Visual Studio 2005 for some old projects that have not yet been converted, and they open very slowly: read this post on If Your Visual Studio Solutions Open Slowly Check WebsiteCache by Thomas F. Abraham.
Emptying your WebsiteCache directory solves the issue: it had about 30-thousand empty directories in it.
The location depends on your Windows version:
- Windows XP, Windows Server 2003 and below:
“%USERPROFILE%\Local Settings\Application Data\Microsoft\WebsiteCache”
- Windows Vista, Windows Server 2008 and up:
“%USERPROFILE%\AppData\Local\Microsoft\WebsiteCache”
This bug has been fixed in Visual Studio 2008 and up.
–jeroen
via: If Your Visual Studio Solutions Open Slowly, Check WebsiteCache | Thomas F. Abraham – On Technology.
Posted in .NET, C#, C# 2.0, Development, Software Development, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2010/08/10
It is good to see the cross breeding effect works:
Last week, the Visual Studio 2010 Productivity Power Tools added Quick Access Extension, which is the Visual Studio equivalent of the Delphi RAD Studio IDE Insight.
It is a keyboard shortcut (Delphi: F6 or Ctrl-.; Visual Studio: Ctrl+3) to search and execute things defined by the environment:
- menu options
- configuration options
- templates
- …
Delphi has a few options that Visual Studio hasn’t and vice versa, but it comes really close.
–jeroen
Posted in .NET, Delphi, Development, Software Development, Visual Studio and tools | 2 Comments »
Posted by jpluimers on 2010/07/27
Like Keith Barrows, each time I see a message like below, I’m reminded that I forgot to change my Visual Studio 2005/2008/2010 to disable these kinds of MDA messages:
ContextSwitchDeadlock was detected
Message: The CLR has been unable to transition from COM context 0x1a7728 to COM context 0x1a75b8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.
Steps to get rid of these messages:
- Start Visual Studio
- In the menu, select “Debug”; “Exceptions…” (Ctrl-D, E)
- Open the “Managed Debugging Assistants” tree
- Uncheck the first checkbox in the “ContextSwitchDeadlock” row
Maybe I won’t forget this next time :>
–jeroen
Via: Keith Barrows : ContextSwitchDeadlock was detected Message.
Posted in .NET, Development, Software Development, Visual Studio and tools | 1 Comment »
Posted by jpluimers on 2010/06/17
When you do some maintenance on old projects, you sometimes bump into things you have completely forgotten about.
This time it is in Visual Studio 2005, with a WPF app, and messages about App.xml.
Since there are few threads covering this problem, so I’m not sure how many people bump into this.
I know that the problem does not limit itself to C#; I have seen people in VB.NET and Delphi.net bump into this as well.
This problem is not limited to Visual Studio 2005, some people also have it in Visual Studio 2010.
Some people also have it with other objects than App.xaml (like Windows1.xaml, etc).
If you get this error, the solution is simple:
- perform a “Clean Solution”,
- then run your app again.
This trick has worked for me every time I bumped into it. Read the rest of this entry »
Posted in .NET, C#, Delphi, Development, Prism, Software Development, Visual Studio and tools, WPF | 2 Comments »
Posted by jpluimers on 2010/06/04
Recently I had to do some work in Visual Studio 2005 and TFS 2005.
I missed the “Folder Compare” feature on one of my machines, and I was sure it was on one of my others.
This feature is standard in Visual Studio 2008. A very handy feature indeed
Then this post reminded me to install the Team Foundation System Power Tools. Read the rest of this entry »
Posted in .NET, Development, Software Development, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2010/05/21
Scott Dorman udpated his macro to change the target framework version for all projects in a solution to Visual Studio 2010 and published the new macro on CodeProject.
His new macro now supports these target frameworks:
Notes:
- The links are to the download pages of the frameworks; look for “Standalone version” or “Full installer” for non-bootstrap download.
(version 1.1 can be downloaded here, but is not supported in VS2010)
- The “Client Profile” versions are stripped down versions of their “Full” counterpart.
–jeroen
Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Delphi, Development, Prism, Software Development, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2010/04/22
I usually pick a fresh VM for installing an RTM build, but if you have loads of stuff on your (physical) machine, upgrading RC to RTM can be a real time saver.
Stack Overflow has a nice question Upgrading Visual Studio 2010 RC to RTM answered by Danny Thorpe (yes, two links: blog / wikipedia).
The order is really imporant, so lets repeat that here:
- Uninstall all the VS 2010 parts
- Uninstall the .NET Framework Multitarget package.
- Reboot
- Uninstall the .NET Framework client package
- Reboot
–jeroen
Posted in .NET, C#, C# 4.0, Delphi, Development, Prism, Software Development, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2010/04/15
Last week, I posted a C# implementation of the tee filter from Sterling W. “Chip” Camden.
Since then I have modified it slightly.
Not because the implementation is bad, but because some pieces of software play dirty when saving their redirected output.
One of those applications is SubInAcl, otherwise a great tool for showing and modifying ACL and Ownership information of Windows NT objects (files, registry entries, etc).
However, when redirecing output or piping it, it writes a zero byte after each byte of text.
I’m not sure why: it might try to face some Unicode output, or just be buggy.
The new sourcecode is below. You can also download the project and binary as tee.C#.7z (you need the freeware 7zip compression tool to decompress this).
Read the rest of this entry »
Posted in .NET, C#, C# 2.0, CommandLine, Development, Encoding, Software Development, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2010/04/14
Every major release of software will bring great new stuff, but the price of upgrading from a previous version is that some stuff will break.
.NET 4.0 brings a lot of nice stuff as well, but there are a few things that break.
There is a nice Breaking changes in .NET 4.0 – Stack Overflow article on this.
The article is now a community wiki, and refers to these pages:
Since the article is a community wiki, expect it to be updated over time.
I wonder what these changes will bring (and break) in the upcoming Delphi Prism release (the datasheet is out now, the product should be out before the end of may).
–jeroen
PS:
If you do not have an MSDN subscription, but still want to see if things break for you, try one of these:
Scott Guthrie has a nice post on the bells and whistles of VS2010.
Posted in .NET, ASP.NET, C#, C# 4.0, Delphi, Development, Prism, Software Development, Visual Studio and tools, Web Development | 1 Comment »
Posted by jpluimers on 2010/04/12
For MSDN subscribers:
Visual Studio 2010 RTM will be available on MSDN around these times:
- 10:00 PST
- 13:00 EST
- 17:00 UTC
Other time zones: see this Worlclock link.
Sources:
Posted in .NET, Delphi, Development, Software Development, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2010/04/07

The usage of tee - image courtesey of Wikipedia
The tee command stems from a *nix background.
It is a command-line filter that allows you to deviate a stream from the regular stdout/stdin redirected pipeline into a file.
Recently, I needed this in a Windows Embedded Stadard (a.k.a. WES) system for logging purposes.
This way, a post-install-script (similar to the Windows Post-Install Wizard, but command-line based) could log to both the console and a log-file at the same time.
WES is the successor Windows XP Embedded (a.k.a. XPe), which is a modularized version of Windows XP.
Se WES usually means that you don’t have the luxury of everything that Windows XP has.
This in turn means that you need to be careful when selecting external tools: a lot of stuff that works on plain Windows XP won’t work.
There are various Win32 ports of tee available.
This time however, I needed a Unicode implementation, so I searched for a .NET based implementation.
Windows PowerShell 2.0 does contain a tee implementation, but:
- We don’t have the luxury of having PowerShell in our WES image
- PowerShell tee first writes the contents to e temporary file, which interferes with how we build this WES image.
Luckily Sterling W. “Chip” Camden started with such a .NET implementation of tee – in Visual C++ – back in 2005.
Though his TEE page indicates it is based on .NET 1.1, his current implementation is done in Visual Studio 2008 using C++.
Now that is a problem for the targeted WES image: that image is based on .NET 2.0.
But when using Visual C++ in .NET, you need additional run-time libraries (for instance the ones for Visual C++ 2005, or the ones for Visual C++ 2008).
If you don’t have these installed, tee.exe does not start, and you get error messages like this on the command-line:
K:\Post-Install-Scripts>tee
The system cannot execute the specified program.
and entries like this in the Eventlog:
Event Type: Error
Event Source: SideBySide
Event Category: None
Event ID: 59
Date: 01/04/2010
Time: 19:09:22
User: N/A
Computer: MYMACHINE
Description:
Generate Activation Context failed for K:\Post-Install-Scripts\tee.exe. Reference error message: The operation completed successfully.
The odd thing in this error message is “The operation completed successfully”: it didn’t
Anyway: translating the underlying C++ code to C# is pretty straightforward, so:
The C# implementation
I did change a few things, none of them major:
- replaced some for statements with foreach
- renamed a few variables to make them more readable
- added using statements for stdin and stdout
- added try…finally for cleaning up the binary writers
- moved the logic for duplicate filenames into a separate method, and moved the moment of checking to the point of adding the filename to the filenames
- moved the help into a separate method
- added support for the -h (same behaviour as –help or /?) command-line argument
The implementation is pretty straightforward:
- Perform parameter parsing
- Catch all input bytes from the stdin stream
- Copy those bytes to both the stdout stream, and the files specified on the command-line
- Send errors to the stderr stream
- Do the proper initialization and cleanup
This is the C# code:
using System;
using System.IO;
using System.Collections.Generic;
// Sends standard input to standard output and to all files in command line.
// C# implementation april 4th, 2010 by Jeroen Wiert Pluimers (http://wiert.wordpress.com),
// based on tee Chip Camden, Camden Software Consulting, November 2005
// ... and Anonymous Cowards everywhere!
//
// TEE [-a | --append] [-i | --ignore] [--help | /?] [-f] [file1] [...]
// Example:
// tee --append file0.txt -f --help file2.txt
// will append to file0.txt, --help, and file2.txt
//
// -a | --append Appends files instead of overwriting
// (setting is per tee instance)
// -i | --ignore Ignore cancel Ctrl+C keypress: see UnixUtils tee
// /? | --help Displays this message and immediately quits
// -f Stop recognizing flags, force all following filenames literally
//
// Duplicate filenames are quietly ignored.
// Press Ctrl+Z (End of File character) then Enter to abort.
namespace tee
{
class Program
{
static void help()
{
Console.Error.WriteLine("Sends standard input to standard output and to all files in command line.");
Console.Error.WriteLine("C# implementation april 4th, 2010 by Jeroen Wiert Pluimers (http://wiert.wordpress.com),");
Console.Error.WriteLine("Chip Camden, Camden Software Consulting, November 2005");
Console.Error.WriteLine(" ... and Anonymous Cowards everywhere!");
Console.Error.WriteLine("http://www.camdensoftware.com");
Console.Error.WriteLine("http://chipstips.com/?tag=cpptee");
Console.Error.WriteLine("");
Console.Error.WriteLine("tee [-a | --append] [-i | --ignore] [--help | /?] [-f] [file1] [...]");
Console.Error.WriteLine(" Example:");
Console.Error.WriteLine(" tee --append file0.txt -f --help file2.txt");
Console.Error.WriteLine(" will append to file0.txt, --help, and file2.txt");
Console.Error.WriteLine("");
Console.Error.WriteLine("-a | --append Appends files instead of overwriting");
Console.Error.WriteLine(" (setting is per tee instance)");
Console.Error.WriteLine("-i | --ignore Ignore cancel Ctrl+C keypress: see UnixUtils tee");
Console.Error.WriteLine("/? | --help Displays this message and immediately quits");
Console.Error.WriteLine("-f Stop recognizing flags, force all following filenames literally");
Console.Error.WriteLine("");
Console.Error.WriteLine("Duplicate filenames are quietly ignored.");
Console.Error.WriteLine("Press Ctrl+Z (End of File character) then Enter to abort.");
}
static void OnCancelKeyPressed(Object sender, ConsoleCancelEventArgs args)
{
// Set the Cancel property to true to prevent the process from
// terminating.
args.Cancel = true;
}
static List<String> filenames = new List<String>();
static void addFilename(string value)
{
if (-1 == filenames.IndexOf(value))
filenames.Add(value);
}
static int Main(string[] args)
{
try
{
bool appendToFiles = false;
bool stopInterpretingFlags = false;
bool ignoreCtrlC = false;
foreach (string arg in args)
{
//Since we're already parsing.... might as well check for flags:
if (stopInterpretingFlags) //Stop interpreting flags, assume is filename
{
addFilename(arg);
}
else if (arg.Equals("/?") || arg.Equals("-h") || arg.Equals("--help"))
{
help();
return 1; //Quit immediately
}
else if (arg.Equals("-a") || arg.Equals("--append"))
{
appendToFiles = true;
}
else if (arg.Equals("-i") || arg.Equals("--ignore"))
{
ignoreCtrlC = true;
}
else if (arg.Equals("-f"))
{
stopInterpretingFlags = true;
}
else
{ //If it isn't any of the above, it's a filename
addFilename(arg);
}
//Add more flags as necessary, just remember to SKIP adding them to the file processing stream!
}
if (ignoreCtrlC) //Implement the Ctrl+C fix selectively (mirror UnixUtils tee behavior)
Console.CancelKeyPress += new ConsoleCancelEventHandler(OnCancelKeyPressed);
List<BinaryWriter> binaryWriters = new List<BinaryWriter>(filenames.Count); //Add only as many streams as there are distinct files
try
{
foreach (String filename in filenames)
{
binaryWriters.Add(new BinaryWriter(appendToFiles ?
File.AppendText(filename).BaseStream :
File.Create(filename))); // Open the files specified as arguments
}
using (BinaryReader stdin = new BinaryReader(Console.OpenStandardInput()))
{
using (BinaryWriter stdout = new BinaryWriter(Console.OpenStandardOutput()))
{
Byte b;
while (true)
{
try
{
b = stdin.ReadByte(); // Read standard in
}
catch (EndOfStreamException)
{
break;
}
// The actual tee:
stdout.Write(b); // Write standard out
foreach (BinaryWriter binaryWriter in binaryWriters)
{
binaryWriter.Write(b); // Write to each file
}
}
}
}
}
finally
{
foreach (BinaryWriter binaryWriter in binaryWriters)
{
binaryWriter.Flush(); // Flush and close each file
binaryWriter.Close();
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine(String.Concat("tee: ", ex.Message)); // Send error messages to stderr
}
return 0;
}
}
}
Some alternatives that might (or might not) support unicode:
http://www.commandline.co.uk/mtee/
http://unxutils.sourceforge.net/ (cannot be downloaded any more – pitty, as they were pretty good)
–jeroen
Update: 201009041030 – Syntax highlighting didn’t work, so changed
sourcecode language=”C#“
into
sourcecode language=”csharp“
Posted in .NET, C#, C# 2.0, CommandLine, Development, Encoding, Power User, Software Development, Unicode, UTF-8, Visual Studio and tools, XP-embedded | 10 Comments »
Posted by jpluimers on 2010/02/12
Google published an interesting graph generated from their internal data based on their indexed web pages.
A quick summary of popular encodings based on the graph:
- Unicode – almost 50% and rapidly rising
- ASCII – 20% and falling
- Western European* – 20% and falling
- Rest – 10% and falling
Conclusion: if you do something with the web, make sure you support Unicode.
When you are using Delphi, and need help with transitioning to Unicode: contact me.
–jeroen
* Western European encodings: Windows-1252, ISO-8859-1 and ISO-8859-15.
Reference: Official Google Blog: Unicode nearing 50% of the web.
Edit: 20100212T1500
Some people mentioned (either in the comments or otherwise) that a some sites pretend they emit Unicode, but in fact they don’t.
This doesn’t relieve you from making sure you support Unicode: Don’t pretend you support Unicode, but do it properly!
Examples of bad support for Unicode are not limited to the visible web, but also applications talking to the web, and to webservices (one of my own experiences is explained in StUF – receiving data from a provider where UTF-8 is in fact ISO-8859: it shows an example where a vendor does Unicode support really wrong).
So: when you support Unicode, support it properly.
–jeroen
Posted in .NET, ASP.NET, C#, Database Development, Delphi, Development, Encoding, Firebird, IIS, InterBase, ISO-8859, ISO8859, Prism, SOAP/WebServices, Software Development, SQL Server, Unicode, UTF-8, UTF8, Visual Studio and tools, Web Development | 7 Comments »
Posted by jpluimers on 2010/01/19
Recently, I had an issue while validating XML with XSD: validation in .NET using the built in classes in the System.XML namespace, and validation in native Windows using the COM objects exposed by MSXML version 6 (which incidentally ships with the .NET 3.0 framework).
Some documents validating OK in .NET did not validate well with MSXML.
I’ll show my findings below, and try to explain the difference I found, together with my conclusions.
The main conclusion is that MSXML version 6 has a bug, but I wonder why I can’t find much more information on it.
Since there is not so much ready to use for validating XML by XSD in .NET and native, I’ll include complete source code of command-line validations applications for both platforms.
.NET source code is in C#.
Native source code is in Delphi.
Read the rest of this entry »
Posted in .NET, C#, C# 2.0, C# 3.0, Delphi, Development, Software Development, Visual Studio and tools, XML, XML/XSD, XSD | 4 Comments »
Posted by jpluimers on 2010/01/12
Finally someone who explains this topic well: CodeProject: Multi-Threading in ASP.NET.
Most of it is based on Web 405 “Building Highly Scalable ASP.NET Web Sites by Exploiting Asynchronous Programming Models” by Jeff Prosise, which should be here on the Microsoft events site (which currently has connection problems) and is referenced here and here.
Recommended reading!
–jeroen
Posted in .NET, ASP.NET, C#, C# 2.0, C# 3.0, C# 4.0, Development, IIS, Software Development, Visual Studio and tools | 2 Comments »
Posted by jpluimers on 2010/01/05
Gerrit Beuze just announced the new beta of ModelMaker Code Explorer 8.
For me, ModelMaker Code Explorer (especially at a price of only EUR 99!) is an indispensable tool for both creating new sources, and maintaining old sources (the refactorings it can do are awesome, but there are many other useful features in it as well).
Over the years, I’ve been using interfaces in Delphi more and more.
Actually, in some of my projects almost all classes implement interfaces.
Therefore, I’m particularly glad with the new feature ’auto complete style drop down list’ in the ‘Edit Class dialog’ that this beta brings.
More info: ModelMaker Code Explorer 8.02 beta.
Note: if you use Visual Studio, there is a Visual Studio edition of ModelMaker Code Explorer too.
–jeroen
Posted in C# 2.0, C# 3.0, Delphi, Development, Software Development, Visual Studio and tools | Leave a Comment »