在带有Windows 10 iot的覆盆子pi存储上创建txt文件

时间:2017-11-20 09:24:15

标签: c# raspberry-pi windows-10-iot-core

我正在使用raspberry pi 2 mod b和最新的Windows iot版本创建者更新。我创建了在监视器上显示数据并在Pi上部署的应用程序。它正在Pi上显示UI。我需要将这些数据保存在Pi存储上的文本文件中,但我无法做到这一点。

有些人请帮助并提供在Pi存储上使用C#中的win 10 iot创建文本文件的详细信息或代码。

我尝试的是方法,但没有运气。

Windows IoT Raspberry Pi 3 c# Create .txt file

Saving files on Raspberry PI with Windows IoT

1 个答案:

答案 0 :(得分:1)

要访问可移动存储,您需要添加以下相关功能:

enter image description here 并声明文件类型如下:

enter image description here

要访问SD卡(操作系统映像卡)的U盘,您需要set folder permissions for UWP app,如下所示:

FolderPermissions U:\mytest -e

利用PInvoke并使用WIN32 API CreateFile和WriteFile来访问TXT文件。要使用这些API,您需要在项目属性中启用不安全的代码,如下所示:

enter image description here

以下是您可以参考的示例。

    /*Part1: preparation for using Win32 apis*/
    const uint GENERIC_WRITE = 0x40000000;
    const uint CREATE_ALWAYS = 2;
    System.IntPtr handle;

    [System.Runtime.InteropServices.DllImport("kernel32", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
    static extern unsafe System.IntPtr CreateFile
    (
        string FileName,          // file name
        uint DesiredAccess,       // access mode
        uint ShareMode,           // share mode
        uint SecurityAttributes,  // Security Attributes
        uint CreationDisposition, // how to create
        uint FlagsAndAttributes,  // file attributes
        int hTemplateFile         // handle to template file
    );

    [System.Runtime.InteropServices.DllImport("kernel32", SetLastError = true)]
    static extern unsafe bool WriteFile
    (
        System.IntPtr hFile,      // handle to file
        void* pBuffer,            // data buffer
        int NumberOfBytesToWrite,  // number of bytes to write
        int* pNumberOfBytesWirtten,  // number of bytes written
        int Overlapped            // overlapped buffer
    );


    [System.Runtime.InteropServices.DllImport("kernel32", SetLastError = true)]
    static extern unsafe bool CloseHandle
    (
        System.IntPtr hObject // handle to object
    );

    public bool Open(string FileName)
    {
        // open the existing file for reading       
        handle = CreateFile
        (
            FileName,
            GENERIC_WRITE,
            0,
            0,
            CREATE_ALWAYS,
            0,
            0
        );

        if (handle != System.IntPtr.Zero)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public unsafe int Write(byte[] buffer, int index, int count)
    {
        int n = 0;
        fixed (byte* p = buffer)
        {
            if (!WriteFile(handle, p + index, count, &n, 0))
            {
                return 0;
            }
        }
        return n;
    }

    public bool Close()
    {
        return CloseHandle(handle);
    }
    /*End Part1*/

    /*Part2: Test writing */
    private void WriteFile()
    {
        string curFile = @"U:\mytest\testfile.txt";

        string teststr = "Wirte here for testing";

        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(teststr);

        if (Open(curFile))
        {
            int bytesWrite;

            bytesWrite = Write(buffer, 0, buffer.Length);
            System.Diagnostics.Debug.WriteLine("Write bytes count:{0}", bytesWrite);

            Close();
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("Failed to open requested file");
        }

    }
    /*End Part2*/

注意:以下是一些可能无法发布到Store的不安全代码。但是如果你只是在Windows IoT核心上使用它,那就没关系了。