我在VS2015中创建了一个UWP应用程序并将其部署到我的Windows 10 IOT设备(RPI 3)。
已部署到此文件夹:
C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\
现在,当它运行时,它没有文件访问权限。
我已尝试在its own directory
中写入,从c:\data
读取,从c:\mydir
读取(新创建,给予每个用户完全访问权限)但无权读取(或写)。
奇怪的是,我的应用程序运行的帐户所在的所有代码示例都不适用于iot应用程序。
答案 0 :(得分:1)
我试图在自己的目录中写作
The app's install directory is a read-only location.
从c:\ data读取,从c:\ mydir读取(新创建,给定 每个用户完全访问权限)但没有权限阅读(或写入)。
要使用Win32 api ReadFile,您需要使用PInvoke。像这样的代码:
/*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*/
注意:以下是一些可能未发布到Store的不安全代码。但是如果你只是在Windows IoT核心上使用它,那就没关系了。