0

我的 UWP 应用程序中有以下代码

public static class DeviceIoControlHelper
    {
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
        private static extern SafeFileHandle CreateFile(
           string lpFileName,
           [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
           [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
           IntPtr lpSecurityAttributes,
           [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
           [MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
           IntPtr hTemplateFile);

public static SafeFileHandle ReturnFileHandler()
        {
            const string drive = @"\\.\LCD";

            SafeFileHandle hddHandle = CreateFile(drive, FileAccess.Read, FileShare.None, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);

            if (hddHandle.IsInvalid)
            {
                int lastError = Marshal.GetLastWin32Error();
                string errorMessage = string.Format(@"!! Invalid {0}; Error ({1}): {2}", drive, lastError, new Win32Exception(lastError).Message);
                throw new Win32Exception(errorMessage);
            }

            return hddHandle;
        }
}

但是,当我尝试从 MainPage.xaml.cs 访问它时,我得到了“拒绝访问”的异常。将 Visual Studio 2015 社区切换到管理员模式也没有帮助

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            try
            {
                DeviceIoControlHelper.ReturnFileHandler();
            }
            catch(Exception ex)
            {

            }
        }
}

我在 Visual Studio 2015 社区中使用 UWP 和 C#

4

1 回答 1

0

您将需要使用CreateFile2通过通用 Windows 应用程序打开文件。但是,它不允许您打开设备。引用 MSDN: 当从 Windows 应用商店应用程序调用时,CreateFile2 被简化。您只能打开 ApplicationData.LocalFolder 或 Package.InstalledLocation 目录中的文件或目录。

于 2016-04-19T21:18:38.960 回答