How to construct WMI query

Try this: string wmiQuery = string.Format(“SELECT CommandLine FROM Win32_Process WHERE Name LIKE ‘{0}%{1}'”, param1, param2); Adding some test info: string wmiQuery = string.Format ( “SELECT Name, ProcessID FROM Win32_Process WHERE Name LIKE ‘{0}%{1}'”, “wpf”, “.exe” ); Console.WriteLine ( “Query: {0}”, wmiQuery ); ManagementObjectSearcher searcher = new ManagementObjectSearcher ( wmiQuery ); ManagementObjectCollection retObjectCollection = searcher.Get ( … Read more

Currency Formatting MVC

You could decorate your GoalAmount view model property with the [DisplayFormat] attribute: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = “{0:c}”)] public decimal GoalAmount { get; set; } and in the view simply: @Html.EditorFor(model => model.Project.GoalAmount) The second argument of the EditorFor helper doesn’t do at all what you think it does. It allows you to pass additional … Read more

Pad left or right with string.format (not padleft or padright) with arbitrary string

There is another solution. Implement IFormatProvider to return a ICustomFormatter that will be passed to string.Format : public class StringPadder : ICustomFormatter { public string Format(string format, object arg, IFormatProvider formatProvider) { // do padding for string arguments // use default for others } } public class StringPadderFormatProvider : IFormatProvider { public object GetFormat(Type formatType) … Read more

vsprintf or sprintf with named arguments, or simple template parsing in PHP

Late to the party, but you can simply use strtr to “translate characters or replace substrings” <?php $hours = 2; $minutes = 24; $seconds = 35; // Option 1: Replacing %variable echo strtr( ‘Last time logged in was %hours hours, %minutes minutes, %seconds seconds ago’, [ ‘%hours’ => $hours, ‘%minutes’ => $minutes, ‘%seconds’ => $seconds … Read more

named String.Format, is it possible?

No, but this extension method will do it static string FormatFromDictionary(this string formatString, Dictionary<string, string> valueDict) { int i = 0; StringBuilder newFormatString = new StringBuilder(formatString); Dictionary<string, int> keyToInt = new Dictionary<string,int>(); foreach (var tuple in valueDict) { newFormatString = newFormatString.Replace(“{” + tuple.Key + “}”, “{” + i.ToString() + “}”); keyToInt.Add(tuple.Key, i); i++; } return … Read more

Is String.Format as efficient as StringBuilder

NOTE: This answer was written when .NET 2.0 was the current version. This may no longer apply to later versions. String.Format uses a StringBuilder internally: public static string Format(IFormatProvider provider, string format, params object[] args) { if ((format == null) || (args == null)) { throw new ArgumentNullException((format == null) ? “format” : “args”); } … Read more