【问题标题】:Write a CSV file to C:\temp running Windows 10 IoT from a Universal Windows App从通用 Windows 应用程序将 CSV 文件写入运行 Windows 10 IoT 的 C:\temp
【发布时间】:2019-07-20 09:50:19
【问题描述】:

当 Raspberry Pi 3(运行 Windows 10 IoT)无法将数据发送到 Azure 数据库时,我一直在编写一个需要将一些数据转储到 c:\temp\TempData.csv 的应用程序。

到目前为止,我已经能够使用 Windows Powershell 创建文件夹和文件,但是当我尝试从应用程序将数据保存到文件时,我只得到“System.UnauthorizedAccessException: Access to the path 'C:\ temp' 被拒绝。在 System.IO.WinRTIOExtensions",从这个错误中可以清楚地看出我们正在谈论权限,但我已经尝试更改该文件夹的 ACL:get-acl "c:\temp" 将返回 "temp BUILTIN\Administrators Everyone Allow FullControl...”,所以它应该拥有所有需要的权限。

从应用程序的角度来看,这是我应该将数据发送到文件的代码:

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;
        }

    }

【问题讨论】:

标签: c# csv permissions raspberry-pi3 windows-10-iot-core


【解决方案1】:

这里是 MSDN 论坛上关于File Access on Windows IoT Core 的一般讨论。对于这个问题,您需要使用 FolderPermissions 工具使文件夹可供 UWP 应用访问。请尝试在 PowerShell 中运行以下命令。你的代码对我来说很好用。

FolderPermissions c:\temp -e

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多