简体   繁体   English

如何使用Visual Studio项目中包含的文件?

[英]How do I use a file included in my visual studio project?

In the Visual Studio application I'm creating, I want to include an .xsd file that is used in the application. 在我创建的Visual Studio应用程序中,我想包含一个在该应用程序中使用的.xsd文件。

The xsd file is in the same directory as the rest of my .cs files, and I dragged/dropped it into the Solution Explorer window as an item in my project. xsd文件与其余.cs文件位于同一目录中,我将其作为项目中的一项拖放到“解决方案资源管理器”窗口中。

But in my C# code... how do I make use of it? 但是在我的C#代码中...我该如何利用它? It doesn't seem right to hardcode the location of it on my computer... but just using "myfile.xsd" or ".\\myfile.xsd" or various combinations of that didn't seem to work... 在我的计算机上硬编码它的位置似乎不正确...但是仅使用“ myfile.xsd”或“。\\ myfile.xsd”或它们的各种组合似乎不起作用...

Thanks in advance! 提前致谢!

-Adeena -Adeena

Do you need it to be an actual file? 您是否需要将其作为实际文件? If not, I'd make it an embedded resource (select that in the properties of the item in Visual Studio) and use Assembly.GetManifestResourceStream to load it at execution time. 如果没有,我将其设为嵌入式资源(在Visual Studio中,在项目的属性中选择该资源),并使用Assembly.GetManifestResourceStream在执行时加载它。 That's neat and tidy for deployment purposes, although it does lose you the flexibility of changing it without rebuilding. 尽管这样做确实使您失去了在不重建的情况下进行更改的灵活性,但是这样做对于部署目的而言是整洁的。

There are several ways, but the easiest would be to click the file in Solution Explorer, go the the properties windows and change the Build Action property. 有几种方法,但是最简单的方法是在解决方案资源管理器中单击文件,转到属性窗口,然后更改“构建操作”属性。 Making it content is popular but you could also go the route of an Embedded Resource. 使它成为内容很受欢迎,但是您也可以采用嵌入式资源的方法。

If you want it to be user configurable you should probably make a custom build step that copies the file into a common location such as CommonApplicationData. 如果希望它是用户可配置的,则可能应该执行一个自定义构建步骤,该步骤将文件复制到公共位置,例如CommonApplicationData。 Then reference it from your application in that path. 然后在该路径中从您的应用程序引用它。 You can get access to those special folders with Environment.GetFolderPath(). 您可以使用Environment.GetFolderPath()访问这些特殊文件夹。

http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx

The parameter it takes is an enumeration: 它采用的参数是一个枚举:

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

CommonApplicationData - The directory that serves as a common repository for application-specific data that is used by all users. CommonApplicationData-该目录用作所有用户使用的特定于应用程序的数据的公共存储库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何公开由类库引用的类库,该类库随后作为项目引用包含在Visual Studio中? - How do I expose class libraries referenced by a class library, which is then included as a project reference in Visual Studio? 如何在Visual Studio项目C#中引用包含的文件 - How to refer to included file inside a visual studio project c# 如何将.discomap文件导入到Visual Studio 2012中的项目中? - How do I import a .discomap file into my project in Visual Studio 2012? Visual Studio 2012:如何打开与项目关联的Excel文件? - Visual Studio 2012: How do I open an excel file that is associated with my project? 如何在我的Visual Studio 2012项目中引用XML文件作为项目代码中的数据源? - How can I reference an XML file in my Visual Studio 2012 project as a data source in my project code? 如何将 .ttf 文件导入我的 Visual Studio 2017 Web 项目以用作 CSS 字体系列值? - How do I import a .ttf file into my Visual Studio 2017 web project to be used as a CSS font-family value? 如何将Microsoft Visual C ++ 2010 SP1与我的Visual Studio安装程序项目一起打包 - How do I package Microsoft Visual C++ 2010 SP1 with my Visual Studio installer project XML文件可以包含在Visual Studio 2010项目中,然后在代码中引用吗? - Can an XML file be included in a Visual Studio 2010 project and then referred to in code? 如何手动编辑Visual Studio项目的清单以显式指定引用的程序集? - How do I manually edit the manifest of my Visual Studio project to explicitly specify referenced assemblies? 如何在Visual Studio 2019中将wwwroot目录添加到我的WebAPI项目 - How do I add a wwwroot directory to my WebAPI project in Visual Studio 2019
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM