0

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

derloopkat
  • 6,232
  • 16
  • 38
  • 45
Farhan
  • 91
  • 1
  • 12
  • and, are you getting any error? – derloopkat Nov 20 '17 at 09:29
  • yes when i use methods in the given URL it is giving me error. right now i am not executing. i will paste the error soon. like for this line var removableDevices = KnownFolders.RemovableDevices; – Farhan Nov 20 '17 at 10:19

1 Answers1

1

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

enter image description here and declare file type like this:

enter image description here

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:

enter image description here

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.

Rita Han
  • 9,574
  • 1
  • 11
  • 24
  • 1
    i used method IsolatedStorageFile myIsolatedStorage from below and it worked. https://stackoverflow.com/questions/34385625/saving-files-on-raspberry-pi-with-windows-iot it is creating folder in below directory which is having very less space. `PS C:\Data\Users\DefaultAccount\AppData\Local\Packages\b3428929-4f78-4889-b9e7-c7618a5cb9ca_d7myw6piuy2ct\LocalState>` any help creating files under U directory `Name Used (GB) Free (GB) Provider ---- --------- --------- -------- C 0.96 0.49 FileSystem C:\ D 0.29 0.29 FileSystem D:\ U 1.84 10.89 FileSystem U:\` – Farhan Nov 21 '17 at 16:04
  • Normally, UWP app doesn't have permission to these folders like c:\, you need [Set folder permissions for UWP apps.](https://learn.microsoft.com/en-us/windows/iot-core/manage-your-device/commandlineutils) – Rita Han Nov 22 '17 at 01:49
  • found `FolderPermissions` but it is gives permission on folder i need to provide access to U drive where i have 10GB of space. also method `IsolatedStorageFile.GetUserStoreForApplication()` is only creating files under C drive any idea how to redirect it to U Drive. – Farhan Nov 22 '17 at 02:36
  • Have you tried using KnownFolders.RemovableDevices to access U drive? – Rita Han Nov 22 '17 at 05:40
  • tried before using `ÌsolatedStorageFile` it is giving error when executing line `var drive0 = externalDrives[0];` – Farhan Nov 22 '17 at 15:49
  • Have you added capability and declaration as my answer pointed out? – Rita Han Nov 23 '17 at 05:47
  • yes after capability and declaration i am able to access `PS C:\Data\Users\DefaultAccount`... path also i think `KnownFolders.RemovableDevices;` is use to read the USB drive and i have not attached any removable drive. i wanted to use my memory card space which is having 10GB free in U drive. – Farhan Nov 23 '17 at 08:48
  • @Farhan Finally got what you want. I update my answer and you can check it. – Rita Han Nov 24 '17 at 06:18
  • please change the above answer to same which was before and that was part of the answer. i used `IsolatedStorageFile.GetUserStoreForApplication()` and it was showing the data is adding in c drive. but when i added 500MB file then found that it is actually showing c but it is internally map to U drive and after adding file, U drive space reduced and not C drive. `Thanks a lot for helping. really appreciate your help.` – Farhan Nov 24 '17 at 14:55
  • @Farhan I add the previous part to current answer instead of replacing it because your question not specified storage type(removable or on OS SD card). Hope this will also help others. – Rita Han Nov 27 '17 at 07:01