0

在我的 Windows 商店应用程序中,我以这种方式保存文件:

StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName.Replace('/', '_'),
                CreationCollisionOption.GenerateUniqueName);

现在我想在文件中添加一个标识符,一个字符串,以便我可以在另一个时刻访问这个属性。

我想覆盖 CreateFileAsync 方法,但它不起作用:

public class MyStorageFolder : StorageFolder
{
    public async Task<MyStorageFile> CreateFileAsync(string x)
    {            
        MyStorageFile file =  (MyStorageFile) await ApplicationData.Current.LocalFolder.CreateFileAsync(x.Replace('/', '_'));

        return file;
    }

}

public class MyStorageFile : StorageFile
{
    private string _objectId = string.Empty;
    public string ObjectId
    {
        get { return this._objectId; }
        set { this._objectId = value }
    }
}

我收到错误“无法将类型 StorageFile 转换为 MyStorageFile”...有没有办法做到这一点??!?!

[编辑]:真的很有趣......在运行时我收到一个错误:'MyStorageFolder':不能从密封类型'StorageFolder'派生......所以我需要一种完全替代的方式来存储我需要的信息!!!

4

2 回答 2

1

组成

public class MyStorageFile {
    StorageFile File { get; set; }
    String MyProperty { get; set; }
}

public class MyStorageFolder : StorageFolder {
    public async Task<MyStorageFile> CreateFileAsync(string x)
    {             
        MyStorageFile file = new MyStorageFile();         
        file.File =  (MyStorageFile) await ApplicationData.Current.LocalFolder.CreateFileAsync(x.Replace('/', '_'));
            return file;
    }

}
于 2013-10-11T13:53:59.360 回答
0

就在这里。创建 StorageFile 类扩展并异步读取/写入您的内容。

async public static Task WriteAllTextAsync(this StorageFile storageFile, string content) 
        { 
            var inputStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite); 
            var writeStream = inputStream.GetOutputStreamAt(0); 
            DataWriter writer = new DataWriter(writeStream); 
            writer.WriteString(content); 
            await writer.StoreAsync(); 
            await writeStream.FlushAsync(); 
        }

代码取自以下链接: http ://dotnetspeak.com/2011/10/reading-and-writing-files-in-winrt

于 2013-10-11T13:54:31.910 回答