An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code

ぃ、小莉子 提交于 2019-12-11 02:05:36

问题


private async void lstPlayList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
     await new MessageDialog(lstPlayList.SelectedValue.ToString()).ShowAsync();
     StorageFile mediaFile = await StorageFile.GetFileFromPathAsync(Convert.ToString(lstPlayList.SelectedValue.ToString()));
     var mediaStream = await mediaFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
     PlayerME2.SetSource(mediaStream, mediaFile.FileType);
}

Whats wrong with this code? I m trying to play a media file when the selection changed from the list box. but Getting the error

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code

I was set the admin privillage too.

<requestedExecutionLevel level="requireAdministrator" uiAccess="true" />

i am getting the error as:

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code

WinRT information: Cannot access the specified file or folder (占퀠0). The item is not in a location that the application has access to (including application data folders, folders that are accessible via capabilities, and persisted items in the StorageApplicationPermissions lists). Verify that the file is not marked with system or hidden file attributes.

Additional information: Access is denied.

If there is a handler for this exception, the program may be safely continued.

anything else i missed?


回答1:


Windows Store apps are running in a sandbox and this is why cannot access all files by using a path (e.g. by using a path like C:/test.txt). The WinRT security model prevents you from doing this.

You can only open files from allowed storage locations like ApplicationData.Current.LocalFolder or ApplicationData.Current.RoamingFolder or when the user actively selects a file using the file picker.

You can also specify capabilities so that you can access for example the picture or document library.

If you need to access a file which has been picked by the user and which is "outside the sandbox" use the StorageApplicationPermissions.FutureAccessList property to make the file accessible after app restart.




回答2:


Try this:

You should run your project in administrator mode if you want access to the root directory

Add this to the app manifest:

<requestedExecutionLevel level="requireAdministrator" uiAccess="true"/>


来源:https://stackoverflow.com/questions/22753843/an-exception-of-type-system-unauthorizedaccessexception-occurred-in-mscorlib-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!