visual-studio 如何让 Visual Studio 编译器忽略文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/615058/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make the Visual Studio compiler ignore a file?
提问by VanOrman
Is anyone aware of a way to make visual studio completely ignore a file when compiling? I have a C# solution that has a foo.config file that isn't really a standard config file - it's just a text file. During compiling VS grabs a hold of the file and bombs.
有没有人知道在编译时让visual studio完全忽略文件的方法?我有一个 C# 解决方案,它有一个 foo.config 文件,它并不是一个真正的标准配置文件——它只是一个文本文件。在编译过程中,VS 会控制文件和炸弹。
I'd like for it to act as though it's just a text file. I do not have the option of changing the name of the file.
我想让它表现得好像它只是一个文本文件。我没有更改文件名的选项。
EDIT: Please note that BuildAction does not exclude files from the compiler checking them. It simply decides if the file is compiled into the assembly, whether it's content (like a jpg or something), or whether it is a resource file. For more info: see the MSDN page.
编辑:请注意 BuildAction 不会从编译器检查文件中排除文件。它只是决定文件是否被编译到程序集中,它是内容(如 jpg 或其他东西),还是资源文件。有关更多信息:请参阅 MSDN 页面。
EDIT2: Apparently, if you have a text file that is named foo.config and you have it open while building, VS2005 will pop up an error thinking that the file should be xml. However, if you close the file, VS2005 will ignore it.
EDIT2:显然,如果您有一个名为 foo.config 的文本文件并且在构建时打开它,VS2005 会弹出一个错误,认为该文件应该是 xml。但是,如果您关闭该文件,VS2005 将忽略它。
Solution: Visual Studio validation causes errors if you have a non-compliant file open during build time. For an example of how to turn this off (for HTML), see Scott Guthrie's post. As Allen mentioned, you should also have the Build Action turned to "None". Unfortunately, this will not stop build errors if you have the file open.
解决方案:如果在构建期间打开了不合规的文件,Visual Studio 验证会导致错误。有关如何关闭此功能的示例(对于 HTML),请参阅Scott Guthrie 的帖子。正如 Allen 所说,您还应该将 Build Action 设置为“None”。不幸的是,如果您打开文件,这不会阻止构建错误。
回答by Allen Rice
right click > properties
右键单击> 属性
Build Action: set to "None"
构建操作:设置为“无”
Edit: If you're talking about app.config, you really cant mess with the format of that, you need to put it in a different .config file.
编辑:如果你在谈论 app.config,你真的不能弄乱它的格式,你需要把它放在不同的 .config 文件中。
I just double checked, VS.net doesnt care as long as its not app.config or web.config and the config file build action is set to "None", it will "error" if you have the file open but it will not cause the build to fail or keep it from building the assemblies.
我只是仔细检查了一下,只要它不是 app.config 或 web.config 并且配置文件构建操作设置为“无”,VS.net 就不在乎,如果您打开文件,它会“出错”,但不会导致构建失败或阻止它构建程序集。
Close the file and the errors will go away, similar to the errors you get about HTML markup. The displaying of these "errors" is probably a configurable setting in vs.net
关闭文件,错误就会消失,类似于您遇到的关于 HTML 标记的错误。这些“错误”的显示可能是 vs.net 中的一个可配置设置
回答by zweiterlinde
The action to take depends on the solution and and file type. For instance (in VS2005), in a C++ solution I can right click on the source file name in the solution explorer and view its properties. The first "General" option is "Excluded From Build", which will allow you to exclude the file from the build process without having it excluded from the project altogether.
要采取的操作取决于解决方案和文件类型。例如(在 VS2005 中),在 C++ 解决方案中,我可以在解决方案资源管理器中右键单击源文件名并查看其属性。第一个“常规”选项是“从构建中排除”,这将允许您将文件从构建过程中排除,而无需将其完全从项目中排除。
I pulled up a .config file in a C# solution, and found a "Build Action" option under the Advanced section. That should probably be set to "None".
我在 C# 解决方案中提取了一个 .config 文件,并在“高级”部分下找到了“构建操作”选项。那应该设置为“无”。
回答by Eric Petroelje
Just right click on the file and choose "Exclude from project".
只需右键单击该文件并选择“从项目中排除”。
If you still want to see it in your project, select the project and click the "Show all files" button at the top of the solution explorer. This will show all the files in the directory tree even if they aren't actually part of the project.
如果您仍然希望在您的项目中看到它,请选择该项目并单击解决方案资源管理器顶部的“显示所有文件”按钮。这将显示目录树中的所有文件,即使它们实际上不是项目的一部分。
回答by Alex Reitbort
Are you sure that VS compiling .config file???
你确定 VS 正在编译 .config 文件???
You should check it's Build Action in file options and may be set it to none.
您应该在文件选项中检查它的构建操作,并且可以将其设置为无。

