[英]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.