How to set standard encoding in Visual Studio - visual-studio

I am searching for a way to setup Visual Studio so it always saves my files in UTF-8.
I have only found options to set this project wide. Is there a way to set it Visual Studio wide?

Do you want the files to save as UTF-8 because you are using special characters that would be lost in ASCII encoding? If that's the case, then there is a VS2008 global setting in Tools > Options > Environment > Documents, named Save documents as Unicode when data cannot be saved in codepage. When this is enabled, VS2008 will save as Unicode if certain characters cannot be represented in the otherwise-default codepage.
Also, which files are not being saved as UTF-8? All of my .cs, .csproj, .sln, .config, .as*x, etc, all save as UTF-8 (with signature, the byte order marks), by default.

What
It is possible with EditorConfig.
EditorConfig helps developers define and maintain consistent coding
styles between different editors and IDEs.
This also includes file encoding.
EditorConfig is built-in Visual Studio 2017 by default, and I there were plugins available for versions as old as VS2012. Read more from EditorConfig Visual Studio Plugin page.
How
You can set up a EditorConfig configuration file high enough in your folder structure to span all your intended repos (up to your drive root should your files be really scattered everywhere) and configure the setting charset:
charset: set to latin1, utf-8, utf-8-bom, utf-16be or utf-16le to
control the character set.
You can add filters and exceptions etc on every folder level or by file name/type should you wish for finer control.
Once configured then compatible IDEs should automatically do it's thing to make matching files comform to set rules. Note that Visual Studio does not automatically convert all your files but do its bit when you work with files in IDE (open and save).
What next
While you could have a Visual-studio-wide setup, I strongly suggest to still include an EditorConfig root to your solution version control, so that explicit settings are automatically synced to all team members as well. Your drive root editorconfig file can be the fallback should some project not have their own editorconfig files set up yet.

I work with Windows7.
Control Panel - Region and Language - Administrative - Language for non-Unicode programs.
After I set "Change system locale" to English(United States). My default encoding of vs2010 change to Windows-1252. It was gb2312 before.
I created a new .cpp file for a C++ project, after checking in the new file to TFS the encoding show Windows-1252 from the properties page of the file.

The Problem is Windows/Microsoft applications put byte order marks at the beginning of all your files when saving as non-ASCII types (UTF-8, etc.). Other applications often break when they read these odd Microsoft UTF-8 byte order encoding marks at the top of files.
A perfect example of this problem was triggering quirsksmode in old IE web browsers when encoding in UTF-8 as browsers often displayed XHTML web pages based on what encoding falls at the start of the page. It makes a mess when other applications view those UTF-8 Visual Studio pages.
I usually do not recommend Visual Studio Extensions, but I do this one to fix that issue:
Fix File Encoding: https://vlasovstudio.com/fix-file-encoding/
The FixFileEncoding above install REMOVES the byte order mark and forces VS to save ALL FILES without a Byte Order Mark signature in UTF-8 at the top of all .NET pages. After installing go to Tools > Option then choose "FixFileEncoding". It should allow you to set all saves as UTF-8 . Add "cshtml to the list of files to always save in UTF-8 without the byte order mark as so: ".(htm|html|cshtml)$)".
Now open one of your files in Visual Studio. To verify its saving as UTF-8 go to File > Save As, then under the Save button choose "Save With Encoding". It should choose "UNICODE (Save without Signature)" by default from the list of encodings. Now when you save that page it should always save as UTF-8 without byte order mark at the beginning of the file when saving in Visual Studio.

I don't know of a global setting nither but you can try this:
Save all Visual Studio templates in UTF-8
Write a Visual Studio Macro/Addin that will listen to the DocumentSaved event and will save the file in UTF-8 format (if not already).
Put a proxy on your source control that will make sure that new files are always UTF-8.

It is an old topic, but I'll put here what helped me:
Change Visual Studio default encoding

Related

Visual Studio Resource Editor corrupts rc files with UTF-8 encoding

Visual Studio 2019 and 2022 Resource Editor is able to correctly read and display .rc file in UTF-8 encoding if .rc file is saved without UTF-8 BOM.
The main requirements for it, .rc file must contain valid #pragma code_page.
Example:
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_NEUD)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(65001)
But if you did some changes in textual resource and saves result back to .rc file, it will be corrupted:
all #pragma code_page(65001) will be replaced by your default ANSI code page (e.g. #pragma code_page(1252))
all non ANSI symbol will be replaced by ?.
Is there some way how to work with UTF-8 c++ resource in Visual Studio 2019\2022 Resource Editor without this bug.
P.S.:
I don't use Unicode (UTF-16 LE) encoding due to problems in git with it.
Using UTF-8 without BOM is single way how to compile UTF-8 .rc file in Visual Studio, see link.
Microsoft knows about this bug and they decided not to fix it:
Thank you very much for your feedback. We've decided to take no action on this feedback at the time.
When working with Resource Compiler (.rc) source files, our suggestion has always been to use UTF-16 whenever resources contain strings that cannot be encoded as ASCII. Based on your replies, it looks like switching back to UTF-16 would unblock your experience with Resource Editor, with the caveat that GitHub won't treat .rc files as text.
While eventually we want to support UTF-8 text encodings for .rc files, we currently do not have plans for this work; and for this reason we're closing this issue as Won't Fix.
See
https://developercommunity.visualstudio.com/t/visualstudio-v1590-resource-editor-using-utf-8-bom/384705

Visual Studio web csproj file indenting flipping between tabs and spaces

The indentation of our web .csproj file is being changed to spaces or tabs, depending on the particular installation/user/machine doing the work. For two of us, VS 2019 will force it to indent of 2 spaces (which we want). For the other two developers, their VS will change it to tab indent whenever adding a new file to the project.
Our tab settings are the same for:
C# = Smart / Tab Size 4 / Insert spaces
XML = Smart / Tab size 4 / Keep tabs
We could not find a setting for .csproj files. The behaviour of VS does not seem to match any of the settings we checked.
Is there another item in Tools / Options / Text Editor or elsewhere that governs this?
The reason this matters is, whenever we merge a changed csproj back into the common branch, if there is a difference in indenting, git doubles the size of the file, including both versions. Git doesn't recognise the lines are really the same apart from leading whitespace.
"Tabs to spaces" is a personal preference.
NOT stored in the csproj file(which is committed to get project), but (a) defaulting from your VS preferences; or (b, most likely) overridden in project specific 'Project Preferences' file (the extension eludes me).
Private preferences shouldn't but committed to the git project, and are typically ignored in the git.ignore file.
But, if that for has been stored with your project, it will now be be downloaded as 'read-only' in any pull, overriding any personal changed you've made.
The " .suo" file rings a bell. Add to git.ignore, and delete from the stored GIT project.
It was bothering me as well. Especially when the original csproj file has 2 spaces and any reformatting change it to 4.
I prefer 2 spaces for csproj and you can do it with a help of EditorConfig on a Solution or Project level. If you do it on the Solution level, add the .editorconfig file to your solution folder with the following content:
# All files
[*]
indent_style = space
[*.csproj]
tab_size = 2
You can find more information in Microsoft Documentation for EditorConfig

Adding a resource file to F# windows app causes UAC on app start

I'm trying to embed an application icon in an F# windows application, using the method described in the accepted answer to Changing F# application icon.
When I add the .res file to the application (either by setting Build/Other flags to --win32res:MyResourceFile.res or by setting Application/Resource File), the UAC prompt pops up when I start it.
I've tried:
Embedding an empty .res file (compiling an empty .rc to .res) - same result.
Creating a new console project, changing the output type to Windows and embedding the .res file - same result.
Deleting the .suo files just in case - same result.
What could make the addition of a .res file to an app cause UAC to kick in?
(Using VS2015 w latest updates, F# 4.0, .NET 4.6.1)
Edit:
I have created 2 minimal projects the same way, but one of them has the UAC problem and the other doesn't. I've compared the fsproj and sln files, but comparing them (Notepad++ compare plugin) I see no difference in any project setting.
Project files for ConsoleApp1, which does not have the UAC problem: http://s000.tinyupload.com/index.php?file_id=35796827731424697941
Project files for FixVS2015Update, which does have the problem: http://s000.tinyupload.com/index.php?file_id=30669246856403684470
Screenshot of KDiff3, comparing the .fsproj files:
Only file names and GUIDs are different, unless I've missed something.
Comparing the .sln files:
I can't find any differences there either.

Visual Studio - Remove unused files

Was wondering if there's an extension or macro or something that looks through your solution and gives out a report of which js/css/image files are not being referenced anywhere in code?
I have a large project and over time it has accumulated dust. Other than manually searching for each file's usage, is there any other time saving way?
There's a Visual Studio Extension that searches for unreferenced image files. It finds all image files in your project and then scan all aspx/cs/ashx/css/js files for references. It has not yet been updated for Visual Studio 2012.
http://visualstudiogallery.msdn.microsoft.com/fb7a9b9c-08e1-4bb4-91b4-8e512feb5a1b
Update You will not find a tool that can systematically identify unused resources, because JavaScript, CSS, and image files can be loaded dynamically. This article shows how to load JavaScript and CSS dynamically, and it's a straightforward task in JavaScript to load an image dynamically. It's easy to imagine a scenario in which the image name is loaded from an external data source, or concatenated from another field value plus ".jpg". Clearly any tool that attempted to scan your source to find unreferenced files would miss these resources.
That said, you can search for hard-coded references to .js, .css, and .jpg files using Visual Studio's search by regular expression feature, or by using a high-powered text editor like Notepad++ with a Regular Expression search feature.
For example, to use Visual Studio to search for all files ending in .js that are referenced in ASCX ir ASPX pages, go to Edit/Find and Replace/Find in Files, set the search expression to .js> switch on
"Use Regular Expressions", and set "Look in these file types" to "*.aspx; *.ascx".
("\" escapes the ".", and ">" means end of word, so that "foo.js" is found, but not "foo.jsx". Visual Studio has its own Regular Expression syntax, which is documented here)
In addition, the tools in my original response below can give a good picture of what JS/CSS/IMAGE resources are actually getting used when your site is loaded. When used in conjunction with a testing tool like Selenium, these should allow you to remove resources with confidence.
There are several tools you should look at:
WARI--Web Application Resource Inspector seems the closest fit. According to their website:
WARI scans your web application and examines dependencies between JavaScript functions, CSS styles, HTML tags and images.
The goal is to find unused images as well as unused and duplicated JavaScript functions and CSS styles.
JSCoverage is a code coverage inspector for Javascript.
For CSS, there is an online tool at http://unused-css.com/ and a Firefox extension called DustMeSelectors
The extension in the selected answer above only works in vs2012 while Code Maid works in vs2010 - vs2014:
There is a free extension called Code Maid that "is an open source Visual Studio extension to cleanup, dig through and simplify our C#, C++, F#, VB, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding." Does images as well.
Solution Features
is semi - manual
isn't an extension
looks long but is not complicated or difficult
works for just images or all files
overcomes all the ajax tricks
should work with any project or version of Visual Studio
Steps
Run the website in Chrome with debugging turned on (press F12 before launching website)
Completely exercise the website. Make it download everything that it will ever download.
Go to the Network tab.
On the file grid, Click on any of the images that appear in the list (doesn't matter which one). This will make all column headers but Name go away.
Click on the Name column header, to sort ascending.
Do CTRL+A, CTRL+C to copy all file names.
Paste into a new Google spreadsheet (in google docs) into cell A2.
Repeat steps 6 and 7 if for any reason it didn't copy all the file names. Scroll down.
Go into your actual website images directory (or whatever directory you are interested in comparing) in a command prompt and issue dir /b. Copy this into the clipboard (mark function) and paste into cell B2.
In cell C2, paste this formula =not(isna(VLOOKUP(B2,$A$2:$A$TheBottomOfA,1,false))). Alter TheBottomOfA to be the last used row in column A.
Copy the formula down for all your values in column B.
In cell C1, type Is Used. In cell B1, type Name. Add a data filter on all the values in columns B and C. Set the filter with the mouse in C1 to show only Is Used=FALSE.
Result
What you're looking at in Column B is a list of files you are not using.
Note
I recommend moving the unused files to an offline folder instead of deleting them.
Warning
You still need to use common sense. BRAIN=ON
2022 solution
you can use this extension in vscode to find all unused exports
If you're using a new version of Visual Studio and can't use the extensions, what I did was this:
Exclude all target files.
Find ",,," (this indicates an absolute resource reference) and include each resource found. Note, you should try any unique keywords relevant to your references.
Repeat step 2 until Visual Studio stops opening files (if no new matches are found, already opened documents will come into focus).
Try building project. If you happen to miss a resource, Visual Studio will let you know. In that case, repeat steps 2-4; it is worth nothing this will rarely ever be necessary.
I've only had to do this once and I spent ten minutes, at most.

Changing source files encoding in XCode 4

We are developing primarily in C++ under Windows and write comments in Russian. So our OEM encoding is Cyrillic (Windows).
XCode 4 does not ask to specify file encoding when you add existing file to the project. And you can specify your encoding in the right pannel on per file basis.
How am I supposed to change encoding for more than 200 files?
Thank you.
Select the files (one or more) in the Project Navigator. In the Utility pane, under the Text Settings group you can change the Text Encoding.
Click the file in the Project Navigator, then open the Utility pane. Choose the File Inspector tab and look under the Text Settings group to find the Text Encoding popup.

Resources