1

Is there a shortcut for adding a using directive without having to leave the point where I'm working in the code to scroll up to the top of the file?

For instance, if I don't have using System.IO; and I add string fullPath = System.IO.Path.Combine(path1, path2); to a method where I'm working, I'd like to be able to right-click or something to get the option to add using System.IO; at the top and remove it from the beginning of System.IO.Path.Combine.

I'm know third-party tools like Re-Sharper can enable this, but I thought this would be a simple feature that would be present by VS 2017.

rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • @OrGroman -- Thank you, that's awesome! – rory.ap Jun 08 '17 at 11:37
  • @OrGroman -- Frustrating....it worked once, and now I can't get it to work again. Don't know what I'm doing differently. – rory.ap Jun 08 '17 at 11:49
  • Right-clicking on it always works when you have an IntelliSense error, as does the keyboard shortcut. If it isn't working, then there probably isn't an error. Is it underlined in squiggles? – Cody Gray - on strike Jun 08 '17 at 11:57
  • @CodyGray -- Can you please re-open? I was almost done writing up my own answer as I believe this is a different question now. If you don't agree, you can re-close, certainly. – rory.ap Jun 08 '17 at 11:58
  • In short, this ctrl + . trick doesn't work unless you remove the namespace component from the beginning of the class name. This only works if you have code which already only uses the un-qualified class name (maybe you copy/pasted, or a snippet). I struggled to figure out for a while why I couldn't get it to work, until I realized I had to delete the ns first from the beginning of the class name. – rory.ap Jun 08 '17 at 11:59
  • @CodyGray -- See updated question. – rory.ap Jun 08 '17 at 12:04
  • Not really sure it's an entirely different question, just a subtle variation, but if you already have a good answer, you might as well post it in the answer box, rather than in the question. :-) – Cody Gray - on strike Jun 08 '17 at 12:10
  • @CodyGray I can't post it in the answer box because you closed the question. That immediately grayed out the button. Anyway the key difference here is that the keyboard shortcut simply does nothing at all if you have the fully qualified namespace which confused me for a while (as you can see by my comment where I said it only worked once). I'd like to help others avoid this confusion. – rory.ap Jun 08 '17 at 12:14
  • Err, yeah, I reopened it just before posting that comment. – Cody Gray - on strike Jun 08 '17 at 12:19
  • @CodyGray -- thank you. – rory.ap Jun 08 '17 at 12:30

1 Answers1

0

While I thought this was an exact duplicate of this question, it's actually different. The linked question assumes you have not already provided the fully-qualified class name. For instance, if you happen to have string fullPath = Path.Combine(path1, path2); instead of string fullPath = System.IO.Path.Combine(path1, path2); and without the using directive using System.IO; at the top of the file -- maybe you inserted a code snippet or copy/pasted from somewhere else -- you can place your cursor near the class (Path) and use Ctrl + . to activate the smart tag.

This is different from my question because I have already provided the fully-qualified class name System.IO.Path (because maybe I couldn't have found Path without it). I'd like it to recognize that there's a namespace component that can be converted into a using directive and remove the namespace component from the beginning of Path. If you do already have the namespace component before the class name, the Ctrl + . shortcut, does not work, and there is no explanation provided. The work-around is to type out the fully-qualified class name, then delete the namespace component, and use Ctrl + .. This allows you to make use of the intellisense for getting the class name you're looking for from whatever namespace it resides in, and then use the smart tag to add the using directive.

rory.ap
  • 34,009
  • 10
  • 83
  • 174