1

Is a class file like MyClass.cs.bak used at run-time even though there is a class file by the name of MyClass.cs within the same Visual Studio project?

Both files are included in the project. The project is of Web Application type, so everything compiles into a single dll when deploying. I am not sure if during compilation, the .bak file is ignored.

I am working on a project in Visual Studio, in which some past developer has included both these files within the project.

Sunil
  • 20,653
  • 28
  • 112
  • 197
  • Most likely just a backup file that should be excluded from the project. Have you diff'd it against the other file and seen the differences? – Lloyd Feb 17 '14 at 16:38
  • @Lloyd - There was a difference in only one line. But we use source control, so why would someone even think of creating a .bak file. – Sunil Feb 17 '14 at 16:41
  • VS.net has certain file extensions and it will find all files in the project and if it knows the extension, it will try to do something with it. If the `bak` extension means something for Visual Studio it will try to process it in the certain way. But it is definitely not code file extension. Code files are either `*.cs`, `.vb`, `*.ashx`(for web seites), etc. Check the build action for the `bak` file. If anything, I would think that this is db-related file extension. – T.S. Feb 17 '14 at 16:45
  • @T.S. - This .bak file was actually a C# class file as I described in my post and not a database file. It contained C# code. – Sunil Feb 17 '14 at 16:53
  • @Sunil I understand. But I wouldn't use `*.bak` extension for this. As a mattet of fact, visual studio has specific `*.exclude` extension to do what you do – T.S. Feb 17 '14 at 16:57

3 Answers3

4

If you click on the file in Solution Explorer and look at the Properties window, you'll see a property called "Build Action". This defines whether the file will be treated like code ("Compile"), included as a resource ("Embedded Resource"), or ignored ("None").

When a file is added to a project, the default Build Action is selected based on the file extension. For .bak files, which have no particular meaning to C# projects, the default "None" should be selected, and the file will be ignored when compiling the project.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50
  • 1
    Excellent explanation. When I tried your suggested check, I found the file had a Build Action of Content. – Sunil Feb 17 '14 at 16:46
1

As far as I know (and check), by default, a *.bak file is not considered as a C# class file in a VS Project. It's just another text file which doesn't complied into the assembly as a class module - Therefore, by the way, why you don't get duplicate class names declaration exception.

You can always tell to VS to treat it as a compilable c# file: Properties -> Build Action -> Compile.

It's just look like a backup (bak) source file - just for history purposes, I assume.

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
1

No.

The .bak file is treated as a normal text file.

This is quite easy to test. Create a new class file, with a class name foo.

Now create a new .cs.bak file and type in the same code.

when you compile the project, you would expect a duplicate class declaration error - this does not occur.

Kami
  • 19,134
  • 4
  • 51
  • 63
  • So what is the use of creating a .bak file in this case? It seems it was not needed at all since we use source control. – Sunil Feb 17 '14 at 16:39
  • I can only speculate that it was created as a backup, or archive. But as the project is in version control, it does not make sense to do so. – Kami Feb 17 '14 at 16:41
  • @Kami - Another problem was that the past developer had set the Build Action to Content for this .bak file, when it should have been None. – Sunil Feb 17 '14 at 16:50
  • @Sunil Again we can only speculate. Content does not compile the file but allows the content to be accessed at runtime. See http://stackoverflow.com/a/145769/1603275 – Kami Feb 17 '14 at 16:56