0

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");
    }
}
Mihai
  • 39
  • 1
  • 10
  • Without any code it is hard to help. Show the code that creates the file and the code that reads the file. – venerik Mar 14 '16 at 21:28
  • @venerik I edited it. You can help me now? Thanks! – Mihai Mar 15 '16 at 11:28
  • @Mihai `Tell me what is the best`: Asking this is irrelevant to your original question – Mangesh Mar 15 '16 at 11:36
  • @MangeshGhotage if you know tell me a good answer for my problem with the name of the file because this is my real problem. That was just for my knowledge, I deleted it – Mihai Mar 15 '16 at 17:54

1 Answers1

0

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

Mihai
  • 39
  • 1
  • 10