Windows Store App unable to append text

64 Views Asked by At

I am using the following Code to Read all the lines from a textfile into an array and then checking the first 4 characters of of the line and if it equals something writing that line to a different file.

        StorageFolder storageFolder = KnownFolders.PicturesLibrary;
        String FileName = "APP DataFile.csv";
        int correct = 0;
        var file = await storageFolder.TryGetItemAsync(FileName) as IStorageFile;
        var lines = await FileIO.ReadLinesAsync(file);
        foreach (string line in lines)
        {
            correct += 1;
        String tempf = line.Substring(0, 4);
        pageTitle_Copy.Text = tempf;
            if (tempf == "Ebbs")
            { writetofile("DataFile_SWANSCOMBE.csv", line); }
            else { };
    ____________________________________________________________________________
      private async void writetofile(String FileName, String tempfi)
    {
        StorageFolder storageFolder = KnownFolders.PicturesLibrary;
        String BodyText = tempfi + Environment.NewLine;            
        StorageFile sampleFile = await storageFolder.CreateFileAsync(FileName, CreationCollisionOption.OpenIfExists);
            await FileIO.AppendTextAsync(sampleFile, BodyText);

    }

It seems to work ok but i keep getting the following error

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code

Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

BodyText = "Ebbsfleet Academy,Phase 2,First Floor,GT Classroom,172,726670 - Socket doesn't fit trunking holes apparent please make safe and repair,Ebbsfleet-26-05-2015-11-31-01.jpg\r\n" string CreationCollisionOption.OpenIfExists = OpenIfExists Windows.Storage.CreationCollisionOption FileName = "DataFile_SWANSCOMBE.csv" string + sampleFile = {Windows.Storage.StorageFile} Windows.Storage.StorageFile + storageFolder = {Windows.Storage.StorageFolder} Windows.Storage.StorageFolder + this = {HCP1.SplitPage1} HCP1.SplitPage1

Problem is when I look at the file it is trying to write to it has already sucessfuly written some of the lines to it Sometimes two line and then crash sometimes 3 lines and sometimes 0 lines

the file is not read only

Any ideas?

Mark

0

There are 0 best solutions below