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? 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...
但是在我的C#代码中......我该如何使用它?在我的计算机上硬编码它的位置似乎是不对的......但是只使用“myfile.xsd”或“。\ myfile.xsd”或其各种组合似乎不起作用......
Thanks in advance!
提前致谢!
-Adeena
3 个解决方案
#1
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. That's neat and tidy for deployment purposes, although it does lose you the flexibility of changing it without rebuilding.
你需要它作为一个实际的文件吗?如果没有,我将它作为嵌入式资源(在Visual Studio中的项目属性中选择)并使用Assembly.GetManifestResourceStream在执行时加载它。这对于部署来说是整洁的,尽管它确实失去了在不重建的情况下更改它的灵活性。
#2
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.
有几种方法,但最简单的方法是在解决方案资源管理器中单击该文件,转到属性窗口并更改“构建操作”属性。使其成为内容很受欢迎,但您也可以选择嵌入式资源。
#3
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. Then reference it from your application in that path. You can get access to those special folders with Environment.GetFolderPath().
如果您希望它是用户可配置的,您应该制作一个自定义构建步骤,将文件复制到CommonApplicationData等公共位置。然后从该路径中的应用程序引用它。您可以使用Environment.GetFolderPath()访问这些特殊文件夹。
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
CommonApplicationData - The directory that serves as a common repository for application-specific data that is used by all users.
CommonApplicationData - 用作所有用户使用的特定于应用程序的数据的公共存储库的目录。
#1
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. That's neat and tidy for deployment purposes, although it does lose you the flexibility of changing it without rebuilding.
你需要它作为一个实际的文件吗?如果没有,我将它作为嵌入式资源(在Visual Studio中的项目属性中选择)并使用Assembly.GetManifestResourceStream在执行时加载它。这对于部署来说是整洁的,尽管它确实失去了在不重建的情况下更改它的灵活性。
#2
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.
有几种方法,但最简单的方法是在解决方案资源管理器中单击该文件,转到属性窗口并更改“构建操作”属性。使其成为内容很受欢迎,但您也可以选择嵌入式资源。
#3
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. Then reference it from your application in that path. You can get access to those special folders with Environment.GetFolderPath().
如果您希望它是用户可配置的,您应该制作一个自定义构建步骤,将文件复制到CommonApplicationData等公共位置。然后从该路径中的应用程序引用它。您可以使用Environment.GetFolderPath()访问这些特殊文件夹。
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
CommonApplicationData - The directory that serves as a common repository for application-specific data that is used by all users.
CommonApplicationData - 用作所有用户使用的特定于应用程序的数据的公共存储库的目录。