Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
资源文件夹中XML文件到C#WPF项目中FileStream的路径_C#_Xml_Wpf_Filestream - Fatal编程技术网

资源文件夹中XML文件到C#WPF项目中FileStream的路径

资源文件夹中XML文件到C#WPF项目中FileStream的路径,c#,xml,wpf,filestream,C#,Xml,Wpf,Filestream,在一个简单的WPF项目中,我有一个XML文件——“Students.XML”来查看和添加记录。XML文件位于项目的Resources文件夹中,其属性为构建操作:Resource和始终复制。我可以使用以下方法读取XML: XDocument doc = XDocument.Parse(Properties.Resources.Students); 但要写入它,FileStream需要字符串形式的路径 FileStream fs = new FileStream(Properties.R

在一个简单的WPF项目中,我有一个XML文件——“Students.XML”来查看和添加记录。XML文件位于项目的Resources文件夹中,其属性为构建操作:Resource始终复制。我可以使用以下方法读取XML:

XDocument doc = XDocument.Parse(Properties.Resources.Students);
但要写入它,FileStream需要字符串形式的路径

     FileStream fs = new FileStream(Properties.Resources.Students, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
 // exception in first argument, string path should be specified.
所以,我应该如何从FileStream中的资源文件夹访问xml文件。请提供帮助。

如果您使用的是,则其内容将嵌入到程序集中,因此它们没有文件路径。相反,您应该在项目中添加一个名为
Resources
的文件夹,并在其中添加XML文件。如果这样做,则可以使用以下属性访问它:

public static string ResourceFolderPath
{
    get
    {
        string path, applicationDirectory = Path.GetDirectoryName(
            Assembly.GetEntryAssembly().Location);
        if (applicationDirectory.Contains(Environment.GetFolderPath(
            Environment.SpecialFolder.UserProfile))) path = Path.Combine(
            applicationDirectory, "Resources");
        else path = Path.Combine(Directory.GetParent(applicationDirectory).Parent.
            FullName, "Resources");
        return path;
    }
}

问题解决了!我刚刚在项目中添加了该文件,还将该文件复制到了项目的调试文件夹中。现在我可以简单地访问“filename.xml”


现在,它起作用了。

要不要给选民留下一条评论?否则,投反对票是毫无意义的。
  XDocument doc = XDocument.Load("Students.xml");
  /...../
 FileStream fs = new FileStream("Students.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);