none
[UWP]Exception using FileIO Append method

    Question

  • Hello:

    I am writing a UWP application wth C##, and trying to add lines of text to a file.

    After getting a file object, and then use the AppendTextAsync method from the FileIO class in a loop:

     while (tt < ll)
                        {


                            int decValue = Convert.ToInt32(data[tt+5]);
                            string s = Convert.ToString(decValue + "\r\n");
                            await FileIO.AppendTextAsync(GPSRawFile, s);
                            tt++;
                        }        

    It runs for a while, and then at random I get the following exception:

    System.Exception: unable to remove the file to be replaced (exception from HRESULT:0x8007497

    Can anyone shed light on this?

    Thanks in advance,

    Marcelo


    Thursday, April 11, 2019 2:21 AM

All replies

  • Hello marcelom49,

    This forum is for "Discuss general issues about developing applications for Windows." It is win32 c++ focused.

    Since this issue is UWP related so I'll move it to UWP forum for more professional support.

    Best regards,

    Rita


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 11, 2019 5:57 AM
  • Hi,

    I couldn't reproduce your error in my side. But according to your error message, I guess it only happens when the loop has run for many many times. The GPSfile you are using might have some delay to load into the memory in the long loop and the file might be locked.

    My suggestion is to use using statement and open the file in stream instead of using AppendTextAsync method.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 11, 2019 10:12 AM
    Moderator
  • I need to do the loop because I am getting 128 byte chunks from an Bluetooth LE device, so I am appending

    text  (one line at a time)to the file as I receive it.

    According to your suggestion should I load all the receive data into a stream object, and then stream it to the

    file when it's done?

    Also, I am getting the error every time the ll variable reaches 80 characters. Could it be the file is getting

    more than 80 characters per line so it crashes?

    Thanks in advance,

    Marcelo

    Thursday, April 11, 2019 3:42 PM
  • Hi,

    Yes, my suggestion is to convert all the data into stream and write them into the text file. 

    Have you tried other add text directly to the file more than 80 characters per line to see if give the same error?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 15, 2019 9:37 AM
    Moderator
  • Roy:

    Yes, I will try your suggestion. It's difficult  using these methods since you have no idea what's going on underneath and no way to find out. It really lengthens development time. Just feedback.

    Thanks,

    Marcelo

    Wednesday, April 17, 2019 8:28 PM
  • Hi,

    Yes, you could make a try about it. If you have any problems, please come back to me to see if I could help.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 18, 2019 5:03 AM
    Moderator