Search code examples
c#csvpermissionsraspberry-pi3windows-10-iot-core

Write a CSV file to C:\temp running Windows 10 IoT from a Universal Windows App


i have been writing an app that needs to dump some data to c:\temp\TempData.csv when the Raspberry Pi 3 (running Windows 10 IoT) can't send the data to Azure database.

So far i've been able to create the folder and the file using Windows Powershell, but when i try to save data to the file from the application i just get "System.UnauthorizedAccessException: Access to the path 'C:\temp' is denied. at System.IO.WinRTIOExtensions", from this error is more than clear that we are talking about permissions but i already tryed changing the ACL for that folder: get-acl "c:\temp" will return "temp BUILTIN\Administrators Everyone Allow FullControl...", so it is supposed to have all the needed permissions.

From the side of the app, this is code i have that should be sending the data to the file:

public static async void SaveFileAsync()
    {
        string File = @"c:\temp\TempData.csv";

        for (int i = 0; i < 50; i++)
        {
            var DataPoint = new SensorData
            {
                Temp = GetNewRandom(22, 40),
                Humidity = GetNewRandom(25, 30),
                Pressure = GetNewRandom(90000, 110000)
            };

            await WriteCSVLine(File, DataPoint);
        }
    }

    private static Task WriteCSVLine(string FilePath, SensorData data)
    {
        try
        {
            using (StreamWriter w = File.AppendText(FilePath))
            {
                return w.WriteLineAsync(data.ToString());
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            throw;
        }

    }

Solution

  • Here is a general discussion about File Access on Windows IoT Core on MSDN forum.For this issue, you need to use FolderPermissions tool to make a folder accessible for UWP app. Please try to run the following command in PowerShell. It works fine for me with your codes.

    FolderPermissions c:\temp -e