Developer

Various notes useful to developers only

Regular Expression to extract quoted text

If you have a text enclosed with single or double quotes and optionally containing escaped quotes inside the text, the following regular expression will help you to extract the text: (['"])(?<text>.*?)(?<!\\)\1 For example, parsing text: 'Hello world'  'World\'s test' "World's test" will yield following results: Hello world World\’s test World’s test Technorati Tags: Regular Expressions

Enumerating user privileges

We had some issues in the impersonation code and I needed to enumerate privileges of currently logged in user – before and after impersonation. Since I couldn’t find quickly such code, I wrote it myself, by compiling few pieces I found on net. Hopefully it will be useful. static void EnumRights() { uint length = 0; bool res;   //...

How to split SQL file by GO statement

In some situations when using scripts to create/update database in code, I had to split input SQL script file by GO statements, as SqlCommand doesn’t really understand it. But in some place, I’ve came to a strange-looking code that does exactly that but… in really weird and NOT correct way. So here we go. How NOT to write: //separator char not used in TSQL char ch = '^';   //Ed: str is the text from SQL batch file ...

PowerShell and untrusted domains

I have a few PowerShell scripts that execute various automation tasks on our build machine, like sanity test of the latest build for example. The common scenario is to revert-VMWare-snapshot, copy latest build to target machine, execute whatever needs to be executed. This worked fine until I was asked to do the same tests on different domain, which is not in trust with domain of our build machine. At first glance, I thought that –Credentials parameter in PowerShell will do the trick, but it turned out it doesn’t work at all for functions like Copy-Item or Set-Content. I thought...

Setting property by name with automatic type conversion

I liked the idea of binding to properties in WPF or Workflows. So implemented something similar (although less powerful) in my code: I had a list of tasks and their data context, so I needed somehow to “bind” data from context to properties of tasks. As a part of implementation, I have created a method that allowed me to take an object (task), take one of its properties by name and assign that property a value. Everything in reflection, since property name nor its value is not known at design time and only evaluated at runtime. Remembering that my value is...

Setting WPF Dialog owner from within WinForms application

I had some WPF dialog that I need to show from older WinForms application, or more precisely from some WinForms UserControl. The problem was to set Owner of that dialog, GetWindow() didn’t work (of course) and leaving dialog without an owner, made it possible to disappear when user switch from application to application. And I didn’t want to show it in task bar either. Thankfully, there is a very easy way, which, for some reason, I didn’t find quite fast – use WindowInteropHelper class. Here is an example: ...

Sending E-mail From PowerShell v1.0

While in PowerShell 2 there will be a “Send-Mail” cmdlet (as I heard), I needed something for current version. Here is the self-explaining PowerShell script for sending mails with optional attachments and multiple recipients. Remember to change the hard-coded “mail.server.com” to your own mail server, otherwise nothing will work :). ############################################################## # Send e-mail with optional attachments # function SendEmail { param( $From, $To, $Subject, $Body, ...

Visual Studio 2008 template for Prototype console application

There have been several occurrences where I needed to have something quick UI, to check some set of classes. Building a whole Windows Forms or WPF application seemed to be overkill, specially when I will not be responsible for building UI later, or application will not need UI at all. So what we usually do? Right, create a command line utility, than another one and another one.. Most of them look exactly the same. So some time ago I have created a Visual Studio project template, that will contain all I need in such console applications, and all...

Replacing passwords on application logs

Many applications write various information into log files and sometimes this information is sensitive, e.g. contains user passwords. In my case, I knew that we are logging SQL statements with stored procedures calls and one of the parameters may contain user’s password. So I have been asked to replace such passwords with something neutral. I’ve ended with following regular expression string (works only for stored procedures and alike call logs!): (?<Pre>(pass(word)?|pwd)[^=']*=[^']*'?)(?<Target>[^']+)(?<Post>'?) Technorati Tags: Regular Expressions, CSHarp, C#

Access Violation in DsBrowseForContainer on Windows Vista. The code

Since my post about DsBrowseForContainer problem in Vista, I have been asked several times to provide the code on solution. So here we go – below is the class that I use to browse for OU container, it is written to .NET 3.5, but can be easily adapted to earlier versions as well: public sealed class OUPickerDialog{ public OUPickerDialog() { Caption = "The container picker"; Title = "Select a container:"; } #region Imports ...

Full Developer Archive