views:

32

answers:

1

In a project I have a bad file, and I'd like to keep it. I know, I can use "Exclude from project", but this hides the file (in VS 2010, in VS 2008, it was still visible). But instead, I clicked, under properties, Build Action: None. When I compile, the project compiles, but when I look in the error list, it still shows the errors of this file.

Shouldn't the reported errors exclude the errors of files that have been excluded from the build? I.e., files marked Content are never build. But when a *.cs file is Content, the Error List still shows the errors of this file. This is more than just a nuisance. On large projects, many errors and warnings arise from files that are never compiled, obscuring the errors that matter . What can I do to prevent this?

+2  A: 

The file is actually excluded from build, but not from the background compilation in the IDE that produces the errors in the error list1.

The simplest thing is to comment out the entire file, press Ctrl+A to select everything, and the Ctrl+K, C to comment out the file.

Another option is to use conditional compilation. On top of your source file insert an undefined pre-compiler constant:

#if DONTINCLUDE
...
//code goes here
...
#endif

Yet another option, of course, is to rename the file so that it has a different extension, e.g. .txt. This will make you loose all the IDE features related to C# such as syntax highlighting though.

1You will actually see that the error list contains less errors (as far as I can see only syntax errors) when you set the Build Action to something other than Compile

0xA3
Ah, yes, I figured something like that. I know (and abuse a lot), Ctrl+k, C, but when you want to keep a file intact in the project and want to exclude it from builds (or even: want to keep it as a resource), I'd love to be able to stop the background compilation from happening without tampering the original contents. That isn't that odd to ask, is it? This seems similar, btw, to the CSS-errors that were too apparent in 2008.
Abel
Sounds like the best of both worlds (i.e., highlighting plus not building because of errors you'd like to solve later, or keep as an example) does not exist. The renaming feature is similar to the 2008 feature of *.exclude (the name it gets when excluding a file from the project). Hmm, I think this is just a missing feature, what's otherwise the use of `Build: None`? Anyway, thanks for thinking along :)
Abel
(oh, you removed your comment...) *"You will actually see that the error list contains less errors"* >> yes, indeed. But not when the file is open. Or am I nitpicking now? ;)
Abel
@Abel: No, this is not correct, you will see the errors only if the excluded file is open in the editor. I wouldn't really say that this is a missing feature. It's rather a feature that the IDE shows you the syntax errors. Switching that off is probably so rarely needed that it never made it in the product. Btw, I incorporated the comment in the answer.
0xA3
Thanks, I actually used the compiler constant (but still found it a nuisance), but eventually switched to a meaningful/less extension (*.keep). But you're right, this should be a scarcely needed feature, just the situation on and of itself raises eyebrows when you think of it.
Abel