Search code examples
c#windows-phone-8.1windows-phone-sl-8.1windows-phone-silverlight

Problems with name of file on sdCard WP SL 8.1


I create a file named "Products.txt" on sdCard. When I open it first time its name is "Products[1].txt", second time the name is "Products[2].txt", third time again "Product[1].txt" and so on. I don't understand why because I don't have another file with the same name. My big problem is that I want to write in it and every time I write something it creates a new one with the same problem. Someone know why the name is changed and how to solve this problem? Thanks! Code for first writing:

    StorageFolder myFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync("MyFolder", CreationCollisionOption.ReplaceExisting);
    StorageFile myFile = await myFolder.CreateFileAsync("MyFile.txt", CreationCollisionOption.ReplaceExisting);
    Stream f = await myFile.OpenStreamForWriteAsync();
    using (StreamWriter sw = new StreamWriter(f))
    {
        sw.WriteLine("First");
    }

Code for other writings:

StorageFolder sf = KnownFolders.PicturesLibrary.GetFolderAsync("MyFolder");
StorageFile f = sf.GetFileAsync("MyFile.txt");
using(Stream s = await f.OpenForWriteAsync())
{
    using(StreamWriter sw = new StreamWriter(s, true))
    {
        sw.WriteLine("Others");
    }
}

Solution

  • Just change Stream f = await myFile.OpenStreamForWriteAsync(); with using (Stream f = await myFile.OpenStreamForWriteAsync()) and the problem is solved