Windows phone 8 在WP8中创建文件和文件夹

Windows phone 8 在WP8中创建文件和文件夹,windows-phone-8,Windows Phone 8,我是WP8的新手,正在尝试构建一个从XML文件读取常量变量并将用户更改写入另一个XML文件的应用程序 但是,我无法使用以下代码创建文件夹或文件 public MainPage() { this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; this.writeFile(); } public as

我是WP8的新手,正在尝试构建一个从XML文件读取常量变量并将用户更改写入另一个XML文件的应用程序

但是,我无法使用以下代码创建文件夹或文件

    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;

        this.writeFile();

    }

    public async void writeFile()
    {
        XDocument xDoc = XDocument.Load("Values.xml");
        lblSafakCount.Text = xDoc.Descendants("isFirstUse").FirstOrDefault().Value.ToString();

        string input = @"<isFirstUse>NO</isFirstUse>";
        var replacement = XElement.Parse(input);;

        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input);
        StorageFolder local = ApplicationData.Current.LocalFolder;
        if (local != null)
        {
            StorageFolder sf = await local.CreateFolderAsync("Res", CreationCollisionOption.ReplaceExisting);
            StorageFile file = await sf.CreateFileAsync("val.xml", CreationCollisionOption.OpenIfExists);
            using (var stream = await file.OpenStreamForWriteAsync())
            {
                stream.Seek(0, SeekOrigin.End);
                stream.Write(bytes, 0, bytes.Length);
            }
        }

    }
public主页()
{
this.InitializeComponent();
this.NavigationCacheMode=NavigationCacheMode.Required;
this.writeFile();
}
公共异步void writeFile()
{
XDocument xDoc=XDocument.Load(“Values.xml”);
lblSafakCount.Text=xDoc.substands(“isFirstUse”).FirstOrDefault().Value.ToString();
字符串输入=@“否”;
var replacement=XElement.Parse(输入);;
byte[]bytes=System.Text.Encoding.UTF8.GetBytes(输入);
StorageFolder local=ApplicationData.Current.LocalFolder;
如果(本地!=null)
{
StorageFolder sf=等待local.CreateFolderAsync(“Res”,CreationCollisionOption.ReplaceExisting);
StorageFile file=wait sf.CreateFileAsync(“val.xml”,CreationCollisionOption.OpenIfExists);
使用(var stream=await file.OpenStreamForWriteAsync())
{
stream.Seek(0,SeekOrigin.End);
stream.Write(字节,0,字节.长度);
}
}
}
我已经成功地从Values.xml中读取了isFirstUse参数,所以从文件中读取没有问题。当我尝试一步一步地检查代码时,应用程序在测试期间不会抛出错误,但不会创建文件夹或文件

你能帮忙吗?
谢谢。

以下是我根据您的功能进行的读写操作

如果您想写入SD卡,可以在此处找到更多信息


public异步void MyWriteFile()
{
字符串输入=@“否”;
byte[]bytes=System.Text.Encoding.UTF8.GetBytes(输入);
StorageFolder local=ApplicationData.Current.LocalFolder;
如果(本地!=null)
{
StorageFolder sf=等待local.CreateFolderAsync(“Res”,CreationCollisionOption.ReplaceExisting);
StorageFile file=wait sf.CreateFileAsync(“val.xml”,CreationCollisionOption.OpenIfExists);
使用(var stream=await file.OpenStreamForWriteAsync())
{
stream.Seek(0,SeekOrigin.End);
stream.Write(字节,0,字节.长度);
}
}
}
私有异步void MyReadFile()
{
字节[]字节=新字节[256];
StorageFolder local=ApplicationData.Current.LocalFolder;
如果(本地!=null)
{
StorageFolder sf=await local.CreateFolderAsync(“Res”,CreationCollisionOption.OpenIfExists);
StorageFile file=wait sf.CreateFileAsync(“val.xml”,CreationCollisionOption.OpenIfExists);
使用(var stream=await file.OpenStreamForReadAsync())
{
读取(字节,0,256);
}
}
}

需要函数头,它看起来像这样吗?“公共异步任务MyWriteFunction()”您好,函数头是“private async void writeFile()”,由主函数调用。代码按编程方式工作。可以写入大约30个字节,然后重新读取。你是说它不会在SD卡上创建文件夹/文件吗?作为解决方案的一部分,我编写了这两个函数,可以很好地读写。顺便说一句,我相信对于您的代码,您正在寻找适合本地设置场景的变量。。。再次查看Hi,我发现问题出在我的WP8仿真器上,所以我多次重新安装仿真器并重新运行代码,现在它工作正常。使用独立存储工具确认已创建文件和文件夹。谢谢你的帮助,阿尔帕斯兰