I am trying to format C# code of a WinForms .NET Core 7.0 project in Visual Studio Community:
private void Form1_Load(object sender, EventArgs e)
{
Dictionary<String, String> Dictionary = new Dictionary<String, String>
{
{ "operation", "login" },
{ "phone", "123"},
{ "country","456"} ,
{ "otp", "789"},
{"language","111" }
};
}
I have tried Ctrl+K, Ctrl+D, removing the last brace in the code and putting it back.
The extra spaced are not getting removed. Can these be removed in whole code automatically using a command? If not, is there a plugin / extension that can help in code formatting?
Use the shortcut key Ctrl+f.
Enter [^\S\r\n]{2,} in FIND
The value in the "replace" is empty
Select use regular expression(Alt+E)
Click the Replace All button(Alt+A)
Use the Auto Align shortcut (CTRL+K+D)
ReSharper works with VS Community and will reformat that case by either
removing the ; and re-entering it (format on typing)
or reformatting the whole file via Cleanup Code... on the context menu for the C# file.
You'd have to pay for ReSharper, though, unless you're a student, working on an open-source project, or otherwise qualify for the free license.
Visual Studio Community (and probably Pro and Enterprise) doesn't seem to reformat dictionaries regardless of what you seem to do with a .editorconfig file or what you have set in Tools | Options | Text Editor | C# | Code Style | Formatting | General.
There may be other extensions that do this. Perhaps you could find one on the Visual Studio Marketplace.
Related
I need to set the value of a Visual Studio option found in Visual Studio -> Tools -> Options -> Text Editor -> JavaScript/TypeScript -> EsLint but I can't seem to find the CollectionPath for this option.
GetSubCollectionNames("Text Editor"); yield a number of results, while GetSubCollectionNames("Text Editor\\JavaScript"); yield 0 results.
TL;DR
How would one go about finding the right CollectionPath for the option pictured in the image below?
This is what I'm using currently.
[ImportingConstructor]
internal VSOptions([Import] SVsServiceProvider serviceProvider)
{
var settingsManager = new ShellSettingsManager(serviceProvider);
_writableSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings)
?? throw new Exception(nameof(settingsManager));
var textEditorSubCollections = _writableSettingsStore.GetSubCollectionNames("Text Editor");
var javaScriptSubCollections = _writableSettingsStore.GetSubCollectionNames("Text Editor\\JavaScript");
// TODO: set option value when we have the right CollectionPath
}
The WritabelSettingsStore class used to extend Visual Studio common settings in Visual Studio. You could use GetPropertyNames("Text Editor\JavaScript") to list all writabel settings for JavaScript, where you will find not all Properties under JavaScript sub collections are listed.
The EsLint is not common Visual Studio Settings. It is third part tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs.
So we could not change it directly with WritableSettingsStore class. You need to know how the EsLint added in Visual Studio and then modify its configuration file for Visual Studio.
This is a cosmetic issue but my application has a default icon on the control panel. Many other applications have custom icons.
My application does have custom icons for the menu and task bar.
How can the icon displayed on the Control Panel be changed using Visual Studio 2015 or later?
Update:
There has been a change in how Visual Studio creates installers. I'm not sure when it occurred, but 2015 definitely does not have a "deployment project". The majority of the hits on Google suggest going to the deployment project properties which does not exist under VS 2015 apps.
This was why I included the tag for visual-studio-2015. Sorry, not to have mentioned that in the original question. It would have been good information.
Using the registry is a possibility but the registry path mentioned, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, does not exist. It does sound kludgy to have the application check it's own icon in the registry all the time. It sounds like an installer function to me.
A post on the Microsoft Developer Network provided an answer. It also modifies the registry. I enhanced it by removing hard-coded values for the application name and the icon file.
// These references are needed:
// using System.Reflection;
// using System.Deployment.Application;
// using System.IO;
// using Microsoft.Win32;
private static void SetAddRemoveProgramsIcon(string iconName)
{
// only run if deployed
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed && ApplicationDeployment.CurrentDeployment.IsFirstRun)
{
try
{
string assemblyTitle="";
object[] titleAttributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), true);
if (titleAttributes.Length > 0 && titleAttributes[0] is AssemblyTitleAttribute)
{
assemblyTitle = (titleAttributes[0] as AssemblyTitleAttribute).Title;
}
string iconSourcePath = Path.Combine(System.Windows.Forms.Application.StartupPath, iconName);
if (!File.Exists(iconSourcePath))
{
return;
}
RegistryKey myUninstallKey = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Uninstall");
string[] mySubKeyNames = myUninstallKey.GetSubKeyNames();
for (int i = 0; i < mySubKeyNames.Length; i++)
{
RegistryKey myKey = myUninstallKey.OpenSubKey(mySubKeyNames[i], true);
object myValue = myKey.GetValue("DisplayName");
if (myValue != null && myValue.ToString() == assemblyTitle)
{
myKey.SetValue("DisplayIcon", iconSourcePath);
break;
}
}
}
catch (Exception) { }
}
return;
}
The original article by Robin Shahan is here: RobinDotNet
For WPF application we need to replace the following code
string iconSourcePath = Path.Combine(System.Windows.Forms.Application.StartupPath, iconName);
Replace with below code
string iconSourcePath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Startup), "TestIcon.ico");
I know you want 2015 but others may be looking for this in newer versions, like I was.
In Visual Studio 2019 Community we can go to the properties panel for the main setup project and the top property is AddRemoveProgramsIcon.
I have just come through this case today. I know it is old but will be useful for new seekers. To expose icon in Control Panel do the following:
Make a folder in [solution Folder][Project Folder]\bin\debug\images
Copy your icon in the new folder
In Set Up project always refer to the icon in the new created folder.
Solved my problem easily
I am attempting to write a Visual Studio extension that will analyze the C# code displayed in the editor and possibly update the code based on what I find. This would be on demand (via a menu item), and not using an analyzer and code fix.
There are a number of examples and samples on the Internet, but they all start either with the source code hard-coded in the samples, or create a new document, or look at each file in the VS solution that is open. How do I access the source code from the active editor window?
In a comment to my original question, #SJP gave a link to #Frank Bakker's answer to the question at Calling Roslyn from VSIX Command. This does work as outlined.
#JoshVarty provided a hint of the direction to go in his answer above. I combined that with code provided by #user1912383 for how to get an IWpfTextView answering the question Find an IVsTextView or IWpfTextView for a given ProjectItem, in 2010 RC extension. Here is the code I came up with:
var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
var textManager = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager));
IVsTextView activeView = null;
ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out activeView));
var editorAdapter = componentModel.GetService<IVsEditorAdaptersFactoryService>();
var textView = editorAdapter.GetWpfTextView(activeView);
var document = (textView.TextBuffer.ContentType.TypeName.Equals("CSharp"))
? textView : null;
In a comment after #user1912383's code mentioned above, #kman mentioned that this does not work for document types such as .sql files. It does, however, work for .cs files which is what I will be using it with.
First, you need to install the Microsoft.CodeAnalysis.EditorFeatures.Text package.
Then you need to add the appropriate using statement:
using Microsoft.CodeAnalysis.Text;
Now you can map between Visual Studio concepts (ITextSnapshot, ITextBuffer etc.) and Roslyn concepts (Document, SourceText etc.) with the extension methods found here: https://github.com/dotnet/roslyn/blob/master/src/EditorFeatures/Text/Extensions.cs
For example:
ITextSnapshot snapshot = ... //Get this from Visual Studio
var documents = snapshot.GetRelatedDocuments(); //There may be more than one
I recently inherited a set of code that includes a .mdproj file. After a bit of googling, I was able to determine this is a Mono Development project. What I wasn't able to resolve was if this is meant to be opened in Visual Studio or not. All of the other projects in the solution are C# projects. So:
Is it possible to open a Mono Development project in VS2010?
If not, can I convert it to a C# project?
Should I use VS2010, or should I use MonoDevelop?
Any insight into this would be much appreciated. I am not familiar with MonoDevelop.
Edit: Turns out that Xamarin Studio cannot open the file either..back to googling.
Update:
Opening the file in a text editor reveals the following:
"General"
{
"SccProjectName" = "\"$/BNT_AVM/CAT/SRC/Enhancement/CATApp/CATAnalysisProject\", BYRCAAAA"
"SccLocalPath" = "."
"SccAuxPath" = ""
"SccProvider" = "MSSCCI:Microsoft Visual SourceSafe"
"ProjectIdGuid" = "{F7615CCA-82F9-41F8-BB04-367601CCBE8A}"
"ShowAllFiles" = "T"
}
"Configurations"
{
"Debug|Win32"
{
}
"Release|Win32"
{
}
}
"Folders"
{
}
"Files"
{
"Model1.mdx"
{
"ProjRelPath" = "T"
}
}
"ProjStartupServices"
{
}
"Globals"
{
}
Does anyone recognize the format?
Since you have pasted its content, then it is clear that this .mdproj file is a model project for Rational XDE. It has nothing to do with MonoDevelop, though it uses .mdproj as file extension.
And the reference of Model1.mdx is a UML model.
Check the reference links and you will see where .mdproj and .mdx are mentioned.
However, IBM no longer develops Rational XDE. So you will have to migrate such projects according to IBM's recommendations,
http://www.ibm.com/developerworks/rational/products/xde/
First off all, sorry if this question already exist but my english is limited for this task.
I use Visual Studio on C# and of course, IntelliSense.
When I write a new method, an If or any other "method/constructor", IntelliSense write like that (when I press Tab or Enter to validate the suggestion)
Actual result :
if (true)
{
// code
}
private void Test()
{
// code
}
// ...
What I want :
if (true) {
// code
}
private void Test() {
// code
}
// etc. ...
See what I want to do ?
I prefer the second 'theme', most readable for me and row economizer.
Is an option behind, hidden in the dark side of VS to do that ?
Thanks for help.
Seems you're asking for javascript style formatting here. I found this link that might help.
1.Click Tools | Options…
2.Scroll down to the Text Editor node
3.Expand the C# node
4.Expand the Formatting node
5.Click on the New Lines node
6.You will see a list of options like in the image below which give you full control over when Visual Studio should put your open brace on
a new line
You can set that on Options window:
Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines