Reputation: 23645
I created a UWP application in VS2015 and deployed this to my windows 10 IOT device (RPI 3).
It's deployed to this folder:
C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\
Now when it runs, it has no file access rights.
I've tried to write in its own directory
, to read from c:\data
, to read from c:\mydir
(newly created, given every user full access rights) but no rights to read (or write).
The weird thing is that all the code examples to see under which account my application is running aren't available for iot apps.
Upvotes: 0
Views: 845
Reputation: 9710
I've tried to write in its own directory
The app's install directory is a read-only location.
to read from c:\data, to read from c:\mydir (newly created, given every user full access rights) but no rights to read (or write).
To use Win32 api ReadFile you need utilize PInvoke. Code like this:
/*Part1: preparation for using Win32 apis*/
const uint GENERIC_READ = 0x80000000;
const uint OPEN_EXISTING = 3;
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 ReadFile
(
System.IntPtr hFile, // handle to file
void* pBuffer, // data buffer
int NumberOfBytesToRead, // number of bytes to read
int* pNumberOfBytesRead, // number of bytes read
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_READ,
0,
0,
OPEN_EXISTING,
0,
0
);
if (handle != System.IntPtr.Zero)
{
return true;
}
else
{
return false;
}
}
public unsafe int Read(byte[] buffer, int index, int count)
{
int n = 0;
fixed (byte* p = buffer)
{
if (!ReadFile(handle, p + index, count, &n, 0))
{
return 0;
}
}
return n;
}
public bool Close()
{
return CloseHandle(handle);
}
/*End Part1*/
/*Part2: Test reading */
private void ReadFile()
{
string curFile = @"c:\test\mytest.txt";
byte[] buffer = new byte[128];
if (Open(curFile))
{
// Assume that an ASCII file is being read.
System.Text.ASCIIEncoding Encoding = new System.Text.ASCIIEncoding();
int bytesRead;
do
{
bytesRead = Read(buffer, 0, buffer.Length);
string content = Encoding.GetString(buffer, 0, bytesRead);
System.Diagnostics.Debug.WriteLine("{0}", content);
}
while (bytesRead > 0);
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.
Upvotes: 2