4

Can someone point out some best practices for creating C# projects in visual studio where I can avoid the problems of sending a copy to another developer resulting in unable to build because of various settings not working on another PC.

For example, if I include a reference to a dll on my PC, the other developer has to re-do that.

Graham
  • 7,807
  • 20
  • 69
  • 114
  • 2
    Try to setup on-premise package manager e.g. NuGet. That will help on that particular problem. Or put the dll inside the source control and reference by relative path maybe? – hendryanw Sep 29 '16 at 09:09
  • @Graham the thing missing between you and the other developer is the code repository. If you are familiar with GIT, then give it a try and I'm 99% sure that your codes will sync unless someone of you will not commit any changes – Anonymous Duck Sep 29 '16 at 09:11
  • 4
    Just store all third party libraries you use in visual studio solution folder, and reference them with relative paths. Of course always use nuget packages where possible. And, of course, use source control systems and don't copy anything. – Evk Sep 29 '16 at 09:12
  • @Sherlock Just as a side question, I started using C# very recently and I am primarily a Java/C++ dev, so when developing in Java we can use a `.gitignore` to ignore `class` and `build` files that are temporary. I've noticed that there is no set ignoring scheme for C#, what is the recommended way to only include essential files? – px06 Sep 29 '16 at 09:12
  • @Evk actually visual studio can restore missing packages but don't rely this most of the time, this is a bad practice. – Anonymous Duck Sep 29 '16 at 09:13
  • Well I didn't say store nuget packages in solution folder, just third party libs – Evk Sep 29 '16 at 09:14
  • @px06 sorry for misconception but GIT is not part of .NET it is purely a version control tool. And of course you can ignore files that are not needed or files that are usually overwritten during compilation. Like `bin` and `debug` folders – Anonymous Duck Sep 29 '16 at 09:14
  • @Sherlock Yes I know what git is, but I've just been using it recently and it seems quite counterintuitive to push all the project files, rather than just the essentials (which I haven't yet found a way to ignore without losing any information). – px06 Sep 29 '16 at 09:16
  • @px06 just an advise based on my experience, usually the folders you need to ignore are `obj` and `bin` because this will change during compilation, other files outside these folders must be pushed so that your application will still work when someone checkouts it – Anonymous Duck Sep 29 '16 at 09:18
  • There is not much to be ignored other than the hidden `*.suo` file, the `obj` and `bin` folder and extra files that you put into the project folder for some reasons. However, for some web projects, I even didn't fully exclude the `bin` folder, since I have `*.dll.refresh` files under version control, that hold a relative path to the actual dll location for custom included packages that are not handled with nuget. – grek40 Sep 29 '16 at 09:21
  • Evk's comment about relative paths: Here's a useful link on that: http://stackoverflow.com/questions/1210754/visual-studio-relative-assembly-references-paths – Graham Sep 29 '16 at 10:18

2 Answers2

3
  1. Store your solution under some version control system, like git.
  2. Exclude all build artifacts from this storage. Build artifacts is whatever files which are created during your build process, such as bin folder, obj folder and so on. You don't want to store that under source control, and you don't want to distribute that to other developers.
  3. Exclude all user-specific artifacts, for example .suo files which store your personal settings for given solution, Resharper artifacts and so on.
  4. Exclude all temporary files which are created by running your application in solution folder (if any).
  5. Include all third party libraries that you reference, which are not nuget packages. Store all those libraries in solution folder under source control.
  6. You may include or not include nuget packages folder. Visual Studio can restore those packages itself, but some people think that one day some packages might become not available and prefer to still store those packages under source control. That decision is up to you.
  7. Sometimes your project has some huge files (graphics, video, audio) and so on. It's generally not advisable to store such files in version control systems like git, because they will have to store a copy every time you make any change. However, git for example provides separate storage for such files (https://git-lfs.github.com/), which is advisable to use in such case.
  8. Never copy and send your solution to another developers, only use source control for that. Copy solution back and forth with all its artifacts can lead to many problems.
Evk
  • 98,527
  • 8
  • 141
  • 191
  • 1
    Normally you are right with point 8. but I think its worth mentioning, that a manual copy of the solution should just follow the same logic as the git repository setup (regarding excluded files). There may be cases like "ship the final source to a client when the project is done", where version control based distribution would be extra work. A clean way to do that would be to develop regularily using version control, make sure that the files under version control are sufficient and export a clean copy from version control current state to be shipped. – grek40 Sep 29 '16 at 10:02
  • Maybe, but it's not that easy to exclude all those files manually, so if people copy and pass - they will almost always copy everything. – Evk Sep 29 '16 at 10:04
1

So after the good advice given by the excellent people on Stack Overflow (I love this site). I thought I'd try a simple test. I knocked up a quick "hello world" console app in VS2015.

Did a "Save All" and here are the files that were there:

C:.                                                                                                
│   ConsoleApplication8.sln                                                                        
│                                                                                                  
└───ConsoleApplication8                                                                            
    │   App.config                                                                                 
    │   ConsoleApplication8.csproj                                                                 
    │   Program.cs                                                                                 
    │                                                                                              
    ├───bin                                                                                        
    │   └───Debug                                                                                  
    │           ConsoleApplication8.exe.config                                                     
    │           ConsoleApplication8.vshost.exe                                                     
    │           ConsoleApplication8.vshost.exe.config                                              
    │           ConsoleApplication8.vshost.exe.manifest                                            
    │                                                                                              
    ├───obj                                                                                        
    │   └───Debug                                                                                  
    │       │   ConsoleApplication8.csproj.FileListAbsolute.txt                                    
    │       │   DesignTimeResolveAssemblyReferencesInput.cache                                     
    │       │   TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs                     
    │       │   TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs                     
    │       │   TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs                     
    │       │                                                                                      
    │       └───TempPE                                                                             
    └───Properties                                                                                 
            AssemblyInfo.cs

I then built and ran the project and these are the files that were there after that:

C:.                                                                                                
│   ConsoleApplication8.sln                                                                        
│                                                                                                  
└───ConsoleApplication8                                                                            
    │   App.config                                                                                 
    │   ConsoleApplication8.csproj                                                                 
    │   Program.cs                                                                                 
    │                                                                                              
    ├───bin                                                                                        
    │   └───Debug                                                                                  
    │           ConsoleApplication8.exe                                                            
    │           ConsoleApplication8.exe.config                                                     
    │           ConsoleApplication8.pdb                                                            
    │           ConsoleApplication8.vshost.exe                                                     
    │           ConsoleApplication8.vshost.exe.config                                              
    │           ConsoleApplication8.vshost.exe.manifest                                            
    │                                                                                              
    ├───obj                                                                                        
    │   └───Debug                                                                                  
    │       │   ConsoleApplication8.csproj.FileListAbsolute.txt                                    
    │       │   ConsoleApplication8.csprojResolveAssemblyReference.cache                           
    │       │   ConsoleApplication8.exe                                                            
    │       │   ConsoleApplication8.pdb                                                            
    │       │   DesignTimeResolveAssemblyReferencesInput.cache                                     
    │       │   TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs                     
    │       │   TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs                     
    │       │   TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs                     
    │       │                                                                                      
    │       └───TempPE                                                                             
    └───Properties                                                                                 
            AssemblyInfo.cs

The extra files being:

ConsoleApplication8\bin\Debug\ConsoleApplication8.exe
ConsoleApplication8\bin\Debug\ConsoleApplication8.pdb
ConsoleApplication8\obj\Debug\ConsoleApplication8.csprojResolveAssemblyReference.cache
ConsoleApplication8\obj\Debug\ConsoleApplication8.exe
ConsoleApplication8\obj\Debug\ConsoleApplication8.pdb

So next, I deleted the bin and obj folders and reloaded the solution. The bin and obj folders were recreated as they were before build.

Brilliant - thanks all.

Graham
  • 7,807
  • 20
  • 69
  • 114