Always Run as Administrator

There are lots of reasons why you might need to run an application as administrator. Visual Studio, for example, needs admin privileges to do things like run a project using IIS or launch the Azure Compute Emulator. If you use these tools daily, manually running as Administrator gets tiresome. The straightforward way to automate this is to check the “Run as Administrator” checkbox on a shortcut, but that checkbox doesn’t get used if you’re opening files directly.

Luckily, there’s a not-so-obvious way to perma-enable running as administrator for an executable.

  1. Navigate to the file in Explorer
  2. Right-click the program and choose Troubleshoot Compatibility
    1 troubleshoot-compatibility
  3. A dialog will display that says it’s detecting issues
    2 detecting-issues
  4. When prompted, select the option to Troubleshoot program

    3 troubleshoot-program
  5. Check the box next to The program requires additional permissions
    4 requires-additional-permissions
  6. The next screen tells you that the UAC setting “Run as administrator” has been applied; click Test the program… to launch the application to verify
    5 test-the-program
  7. You can verify that the application is running as administrator because “(Administrator)” will be added to the title bar
    6 running-as-administrator
  8. The troubleshooter will ask you to verify that the problem was fixed; select Yes, save these settings for this program
    7 is-the-problem-fixed
  9. Finally, you’ll be displayed a summary indicating that the “problem” has been fixed, and you can close the troubleshooter
    8 troubleshooting-completed

And–voila!–your application will now launch as administrator regardless of how its started, whether it be through a shortcut to the .exe or by double-clicking a file for which it’s the default program.

Ok Google, You Jacked Up My Skype

The last few times I’ve tried to use Skype from my phone, I’ve had a problem where the person I’m calling says I sound like a chipmunk and they can’t understand anything I say. The first time it happened, I didn’t really think anything of it. I just figured there was something messed up with Skype and/or my phone that would work itself out after some time. But then it happened again a few weeks later, so I figured I should do some troubleshooting.

I tried uninstalling and reinstalling Skype, but no dice–the problem persisted. Then I did what I probably should’ve done first: googled “android skype chipmunk.” The first hit was an AT&T forum with a bunch of people describing my situation. Essentially, “I have a Galaxy S4 and peeps can’t understand me because I’m chipmunk’d.”

The accepted answer on the thread said this:

Think I figured it out – I had enabled ‘OK Google’ from any screen – turning it back off solved the issue.

Gah, for real? I headed over to the Google app and disabled the option to listen for “Ok Google” from any screen. Sure enough, problem solved. This is a disappointing resolution because I really like saying “Ok Google” to initiate and execute voice searches from anywhere, but apparently I have to choose between having that and having to manually turn it off when I use Skype.

After scouring forums for a while longer, I did find a Hangouts thread where some Google folks acknowledged that they were looking into the issue but no word on finding a cause or timeline for a fix. Other posters on the forum cited problems with LG phones. This coupled with the fact that it’s an issue with video chatting in Skype and Hangouts indicate that this is not a Samsung or Skype problem.

So, okay Google… Fix this!

Response Type for Web API Actions that Return IHttpActionResult

Nice, exactly what I was looking for!

If your action produces HttpResponseMessage or IHttpActionResult instead, you can then use the new ResponseTypeAttribute to hint Web API about the return type.

via Return types, action parameters and data annotations now available in Web API 2.1 Help Page – StrathWeb StrathWeb.

Go To Implementation with View Call Hierarchy

Hands down, my favorite thing about ReSharper is that it adds Go To Implementation to the context menu for easy navigation to a function in a class that implements an interface. That’s kind of a wordy statement, so let me rephrase. Consider the example below. If I have a variable foo of type IFoo, ReSharper lets me right-click a call to foo.Bar() and jump directly to the implementation in either BasicFoo or ExtremeFoo.

interface IFoo
{
    public void Bar();
}

class BasicFoo : IFoo
{
    public void Bar()
    {
        Console.WriteLine("BasicFoo.Bar()");
    }
}

class ExtremeFoo : IFoo
{
    public void Bar()
    {
        Console.WriteLine("ExtremeFoo.Bar()");
    }
}

But what if you don’t have ReSharper? I used to think the next best option was Find All References, which does a good job of finding all the right places where you might want to look for an implementation. However, the implementations are mixed with the method itself and calls to the method, and the results returned by Find All References can become rather unruly.

find-all-references

Note that I said “used to think,” though. See that option under Find All References in the context menu, View Call Hierarchy? Use that. If you look at the Call Hierarchy for an interface method, you’ll get a group of implementations–exactly what I love about ReSharper’s Go To Implementation! You’ll also have a group of callers to the method which is what you get from another ReSharper nicety, Find Usages. Awesome.

call-hierarchy

Call Hierarchy is pretty amazing, and it’s kind of a bummer that I didn’t know about it until just now. Find All References still has its uses, but it’s taking a backseat to View Call Hierarchy in my toolkit. If you haven’t used it before, give it a shot–I bet you’ll love it, too.

Retrieving Properties from PSObject ExtensionData Using Reflection

Oh, PowerShell. Why do you do this to me? I run a query from your command line and see a bounty of properties, But, alas, when run from code, the properties that I expect are missing! Where could they be? ExtensionData? Ugh.

Now, it really seems like getting property values out of ExtensionData shouldn’t be so hard. It was difficult and annoying enough to me that I feel like I must be doing something wrong. If you know of a better way, please–PLEASE–let me know. Until then, I present to you a method for accessing the properties using reflection.

In this example, I’m essentially trying to access the following path of a PSObject:

psObject.ExtensionData.Value.Members
    .FirstOrDefault(x => x.Name == "propertyName").Value.Value

Things start off pretty straightforward. We want to get the ExtensionData property, which is included in the PSObject‘s Members collection.

var extensionData = psObject.Members["ExtensionData"].Value;

extensionData has its own Members collection, so we get that next. It’s not a public property, though, so we have to dig it out using reflection. Also note that we cast the object to an IEnumerable<object>

var members = extensionData.GetType()
    .GetProperty("Members", BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(extensionData) as IEnumerable<object>;

Things are starting to get a little trickier. We need to reflect on the members to find the property name that we’re looking for.

var memberType = members.First().GetType();
var nameProperty = memberType.Getproperty("Name", BindingFlags.Public | BindingFlags.Instance);
var member = members
    .Where(x => string.equals(propertyName, nameProperty.GetValue(x) as string, 
        StringComparison.InvariantCultureIgnoreCase))
    .FirstOrDefault();

Now we’re in the home stretch, and we just need to get the property value. One caveat, though: the property is a data node, so you actually need to get its value. That’s right, we need Value.Value.

var valueProperty = memberType.GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
var value = valueProperty.GetValue(member);
valueProperty = value.GetType().GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
return valueProperty.GetValue(value) as string;

It got kinda gross in the end there, but mission accomplished. I found that the data types weren’t preserved in the extension data, so I had to return values as strings and cast to the appropriate data type (e.g., bool) outside my function.

Here’s the complete solution. (Error/null-checks omitted for brevity.)

string GetPropertyFromExtensionData(PSObject psObject, string propertyName)
{
    var extensionData = psObject.Members["ExtensionData"].Value;

    // members = extensionData.Members as IEnumerable<object>
    var members = extensionData.GetType()
        .GetProperty("Members", BindingFlags.NonPublic | BindingFlags.Instance)
        .GetValue(extensionData) as IEnumerable<object>;

    // member = members.Where(x => x.Name == propertyName)
    var memberType = members.First().GetType();
    var nameProperty = memberType.Getproperty("Name", BindingFlags.Public | BindingFlags.Instance);
    var member = members
        .Where(x => string.equals(propertyName, nameProperty.GetValue(x) as string, 
            StringComparison.InvariantCultureIgnoreCase))
        .FirstOrDefault();

    // return member.Value.Value as string
    var valueProperty = memberType.GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
    var value = valueProperty.GetValue(member);
    valueProperty = value.GetType().GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
    return valueProperty.GetValue(value) as string;
}

Visual Studio Fix: Intellisense Not Popping Up or Not Working « OmegaMan’s Musings

I was monkeying around with Resharper optimization and lost Intellisense altogether! Here’s how I got it back.

In quick launch type “Statement Completion” and choose the first one  “Text Editor -> All Languages -> General” to be taken to the options page for all languages.

On the general options page in the “Statement Completion” section override whatever is in the boxes “Auto list members” and “Parameter Information” and makes those checked. Such as shown here:

via Visual Studio Fix: Intellisense Not Popping Up or Not Working « OmegaMan’s Musings.

Follow

Get every new post delivered to your Inbox.

Join 48 other followers

%d bloggers like this: