c#xamarin在usb存储中写入时会获得未经授权的访问异常

时间:2018-04-30 12:18:37

标签: c# xamarin

我想在usb存储中写一个字节的文件, 但我得到一个未经授权的访问异常。 我怎么能解决这个问题?

    UsbReciever usbReciever = new UsbReciever();
    usbManager = (UsbManager) GetSystemService(Context.UsbService);
    device = null;
    PendingIntent mPermissionIntent = PendingIntent.GetBroadcast(this, 0,new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    RegisterReceiver(usbReciever, filter);

    if (usbManager != null)
    {
        foreach (var dev in usbManager.DeviceList)
        {
            device = dev.Value;
        }
        usbManager.RequestPermission(device, mPermissionIntent);
        bool hasPermision = usbManager.HasPermission(device);
        UsbDeviceConnection connection = usbManager.OpenDevice(device);
    }

    try
    {
        FileStream fileStream = new FileStream("/storage/3ACB-22BF/cntl",FileMode.Open);
        fileStream.Seek(1, SeekOrigin.Begin);
        fileStream.WriteByte(cmd);
    }

在这一行,

FileStream fileStream = new FileStream("/storage/3ACB-22BF/cntl",FileMode.Open);

获取未经授权的访问例外。

0 个答案:

没有答案