Stylecop ignores settings file in subfolder - c#

I would like to have StyleCop rules apply on a specific namespace (all classes from this namespace are in the same folder).
My project structure looks like this:
Folder1
Class1.cs
Class2.cs
Settings.StyleCop (namespace settings)
Class3.cs
Class4.cs
Settings.StyleCop (general settings)
The general settings are correctly applied to all files (including the ones in the subfolder), but the namespace settings are completely ignored.
I haven't used StyleCop in a while, but I thought this was possible. Am I doing something wrong?
How can I make this work?

I don't think it's possible with Stylecop. I made some experiments and the rule file in the subfolder was always ignored.
I think it only works on a project level. I didn't go through the source code but placed a new C# file under the project without adding it to the project and it didn't analyze it which suggests it finds the code files by looking at the project file and ignoring any files just lying in that folder.
Also if you delete the Settings.Stylecop file under the project folder and select Stylecop Settings in Visual Studio it automatically creates a new Settings.Stylecop file even if you don't save the settings which also made me think it's highly coupled to the project file.
I'd suggest to move the contents of the Folder1 to a different class library and apply a different settings file to that project. You can merge settings with the other project and decide which rules apply to which project.

Related

Can a gitignore file have a generic pattern for XML documentation files?

Enable the XML documentation file from the C# Project Properties > Build tab.
Build the project
By default, git will suggest adding this to the repo.
Since it's a build artifact, this is not desired.
However, it needs to be manually excluded for each instance in e.g. gitignore files.
Are you really using these XML Document files? We weren't using them, so our solution was just to disable generation of these files in each project (don't forget to update all configurations).
If you are using XML Documentation files, you can put them in your output path and exclude the path. It appears this is the default to go to $(OutDir) or bin\Debug for example. They should be excluded by these rules:
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
If you put the XML files into your project directory for some reason, you could change the filename to add a pattern like $(Project)_XMLDoc.xml and then you could use the pattern:
*_XMLDoc.xml

C# Project what to put in Source control (Bitbucket)

I have created a C# project. I have created a Bitbucket account and want to put my project there.
What all should I put in the repository and what not to.
I am guess Debug and Release folders should not be uploaded. What about..
- Name/bin/Debug
- Name/obj/Debug
Thanks
It's a good idea to start with an established .gitignore file. GitHub has a project that maintains .gitignore files for various environments, including Visual Studio.
https://github.com/github/gitignore
Scroll down and grab VisualStudio.gitignore. Or you can download the file with this PowerShell command (set the current directory to the root of the repo, first):
(Invoke-WebRequest 'https://raw.githubusercontent.com/github/gitignore/master/VisualStudio.gitignore').Content > .gitignore
This should give you an idea of what the general consensus is on what belongs in source control and what doesn't in a Visual Studio solution. In particular, this will exclude the bin and obj folders so you don't commit outputs to the repository, as well as the .vs folder and *.user files, which are user-specific data. It also excludes files and folders used by popular third-party extensions that maintain their own user-specific data.
Once you have a good .gitignore file, you can use git add --all and it will add everything that's not ignored to the index.
Nothing in bin or obj should need to be committed. You'll also need csproj files and nuget package config files (be they xml or json). Things like appconfig files, and .vscode are up to you whether you want to have them in source control.

Specify path to CustomDictionary file for StyleCop spellchecking

Spell checking of the comments was added to the recent versions of StyleCop. It seems I can reuse my existing CustomDictionary file (that I created for FxCop) with StyleCop. SA1650 rule documentation does not say that it is possible. But in release notes for version 4.7.39 I see
Add support for specifying dictionary folders in the settings.StyleCop
file.
How do I configure StyleCop to search for this file in the root folder of my solution?
In my case it worked when I specified the custom dictionary entries in the Settings.StyleCop file located next to the .csproj file.
<GlobalSettings>
<StringProperty Name="MergeSettingsFiles">NoMerge</StringProperty>
<CollectionProperty Name="RecognizedWords">
<Value>word1</Value>
<Value>word2</Value>
...
</CollectionProperty>
</GlobalSettings>
Actually, the StyleCopSettingsEditor.exe utility created these settings for me. I opened it using the project's context menu in Visual Studio, using the "StyleCop Settings" menu item.
Add a file named Settings.StyleCop in your solution root directory with the following content:
<StyleCopSettings Version="105">
<GlobalSettings>
<CollectionProperty Name="DictionaryFolders">
<Value>**my-dictionary-folder**</Value>
</CollectionProperty>
</GlobalSettings>
</StyleCopSettings>
Where you replace my-dictionary-folder with the relative path to the folder containing your CustomDictionary.xml file.
According to the StyleCop documentation for rule SA1650
The CustomDictionary.xml file should be placed in the same folder as the StyleCop.dll and the Rules. That folder (and all subfolders) are checked for the dictionary files.
StyleCop loads CustomDictionary.xml, CustomDictionary.en-GB.xml and then CustomDictionary.en.xml (where en-GB is the culture specified in the Settings.StyleCop file).
StyleCop also loads custom.dic, custom.en-GB.dic and then custom.en.dic (where en-GB is the culture specified in the Settings.StyleCop file).
Recognized words can also be added to the Settings.StyleCop file using the Settings Editor on the spelling tab.
So it appears that you would have to put a copy of CustomDictionary.xml in that specific location rather than in the root of the solution folder.
A similar question has been asked here http://stylecop.codeplex.com/workitem/7422 and there is an open ticket here http://stylecop.codeplex.com/workitem/7435 which I believe will provide what you're looking for if it gets done.

How Can I Change the Rename my Project File and Class files in a smooth way with Visual Studio?

I am finally done with my ASP.NET Webapplicaton and my solution contains three projects: the webapplication itself, WCF and Classlibrary. But I would like to refactor it to change class names, project names, etc. How can I do this in a smooth way so everything doesn't get messed up, like namespaces etc. I also have Resharper but I'm new to that.
When I renamed my Project file and .csproj etc I still have problems with namespaces the new namespace is not working it asks for the old one.
Thanks in advance!
Right click the Class/Object Name(inside the file) > Refactor > Rename.
It'll ask you if you want to preview as well so you know what gets changed and what not.
To my knowledge, there's no streamlined way to do everything you're asking. As jbkkd mentioned, it's easy to rename classes, but you'll have challenges with the following:
Class file names - renaming a class from the source file will not rename the *.cs filename to match (if the file is open for editing and you rename from the Solution Explorer, it should ask you to rename the class)
You can rename a project name from the Solution Explorer, but this will not automatically rename the namespaces in your code or the physical folder names in your solution (where your project files are contained)
Depending on the type of ASP.NET application, there could be auto-generated files and/or code-behind files that may not have their class and/or namespace name changed; for example, if you change the class name or namespace in a Global.asax.cs file, you have to also make the changes in the Global.asax file too as it will not be done automatically
ReSharper will help with renaming classes and namespaces, etc., but you'll still have to manually clean things up and depending on how big your solution is, it could be a project in and of itself.

C# Load Application Settings from other project?

I have a bunch of property settings saved in an application (WinForms btw, I have never touched WPF) in a project, and I'm re-writing that project again due to messy coding. The problem is, there are around 50 settings (the settings which you can see from Project Properties and the Settings option) in that project and I don't know how to copy them to the new project. Is there a way to do this or will I have to add them all over again?
The settings are saved in two settings files (settings.settings and settings.designer.cs)
You can just copy these files to your new project and overwrite the empty ones with the ones from the previous project.
You might need to edit the files before copying them to make sure the namespace matches the namespace on the new project.
In the Solution Explorer open $MyPojectName\Properties\Settings.settings with XML Editor (use Open With... context menu option) and copy them where you want.
And if you want to override another project's settings file be ready to renew
root element's GeneratedClassNamespace attribute.
Go to the Properties directory of the project with the settings you want.
Copy the Settings.Designer.cs and Settings.settings files.
Go to the Properties directory of the new project and paste them.
Open the Settings.Designer.cs (I use notepad for this) and change the namespace to match your new project.
The settings should now be available in your new project. You can view them by clicking on the Settings.settings file in the Solution Explorer.
just copy the app.config file to destination project folder

Categories