USB Device Connected

//using System.Management public bool IsUsbDeviceConnected(string pid, string vid) { using (var searcher = new ManagementObjectSearcher(@”Select * From Win32_USBControllerDevice”)) { using (var collection = searcher.Get()) { foreach (var device in collection) { var usbDevice = Convert.ToString(device); if (usbDevice.Contains(pid) && usbDevice.Contains(vid)) return true; } } } return false; }

Fun (?) with Linq Expressions in extension methods

If you don’t need the title attribute on individual options your code could be simplified to: public static HtmlString SelectFor<TModel, TProperty, TIdProperty, TDisplayProperty, TListItem>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TListItem> enumeratedItems, Expression<Func<TListItem, TIdProperty>> idProperty, Expression<Func<TListItem, TDisplayProperty>> displayProperty, object htmlAttributes ) where TModel : class { var id = (idProperty.Body as MemberExpression).Member.Name; var display = … Read more

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

if (record[“Dosage”].ToString() != string.Empty) result.StartsWith(“https://stackoverflow.com/”) && result.EndsWith(“https://stackoverflow.com/”) ? result.Replace(“https://stackoverflow.com/”, string.Empty) : result; The second line there is not a statement, it is an expression. Not all expressions can be statements in C#, so this is a syntax error. Presumably you meant to assign the result of this to result: if (record[“Dosage”].ToString() != string.Empty) result = … Read more

Cross Origin Resource Sharing for c# WCF Restful web service hosted as Windows service

Finally found a solution to my queries. Its all here. Supporting Cross Origin Resource Nice step by step explanation. I guess I could have never figured this out on my own. CODE: Create 2 classes as follows: MessageInspector implementing IDispatchMessageInspector. BehaviorAttribute implementing Attribute, IEndpointBehavior and IOperationBehavior. With the following details: //MessageInspector Class using System; using … Read more