简体   繁体   中英

Create txt file on raspberry pi storage with windows 10 iot in c#

I am using raspberry pi 2 mod b with latest Windows iot version creator update. I created application which is displaying data on monitor and deployed on Pi. it is showing UI on Pi. I need to save this data in text file on Pi storage but I'm unable to do that.

Some one please help and provide details or code for creating text file on Pi storage with win 10 iot in C#.

What I tried is below methods but no luck.

Windows IoT Raspberry Pi 3 c# Create .txt file

Saving files on Raspberry PI with Windows IoT

To access removable storage you need to add related capability like this:

在此处输入图片说明 and declare file type like this:

在此处输入图片说明

To access U drive of the SD card(OS image card) you need set folder permissions for UWP app like this:

FolderPermissions U:\mytest -e

Utilize PInvoke and use WIN32 APIs CreateFile and WriteFile to access TXT file. For using these APIs you need enable unsafe code in your project properties like this:

在此处输入图片说明

Here is a sample you can reference.

    /*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*/

NOTE: Here are some unsafe code that may not be published to Store. But it is no bother to you if you just use it on Windows IoT core.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM