我正在开发一个uwp平台,允许使用imgur api上传图像 . 我这样做:
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
String path;
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
path = file.Path;
}
else
{
path = "Operation cancelled.";
}
try
{
var client = new ImgurClient("id", "secret");
var endpoint = new ImageEndpoint(client);
IImage img;
await Task.Run(async () =>
{
await Task.Yield();
Debug.Write("crash at FileStream\n");
using (var fs = new FileStream(@path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Debug.Write("crash at Upload\n");
img = await endpoint.UploadImageStreamAsync(fs);
Debug.Write("Image uploaded. Image Url: " + img.Link);
Windows.UI.Xaml.Controls.Image image = new Windows.UI.Xaml.Controls.Image();
image.Source = new BitmapImage(new Uri(img.Link));
image.Width = img.Width;
image.Height = img.Height;
imgList.Clear();
imgList.Add(image);
await LoadGalery();
index = 0;
}
});
}
catch (ImgurException imgurEx)
{
Debug.Write("An error occurred uploading an image to Imgur.");
Debug.Write(imgurEx.Message);
}
我有这个错误:
$ exception {System.UnauthorizedAccessException:拒绝访问路径'C:\ Nouveau dossier \ mmmh.PNG' . 在System.IO.WinRTIOExtensions.d__2`1.MoveNext()---从抛出异常的上一个位置的堆栈跟踪结束---在System.Runtime的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) . System.IO.WinRTFileSystem.d__41.MoveNext()中的CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)---抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务) System.IO上System.IO.WinRTFileSystem.Open(String fullPath,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项,FileStream父级)的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的任务) .MultiplexingWin32WinRTFileSystem.Open(String fullPath,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项,FileStream parent)在System.IO.FileStream.Init(String path,FileMode mode,FileAcc)位于App1.MainPage的System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问,FileShare共享)的ess访问,FileShare共享,Int32 bufferSize,FileOptions选项 . <> c__DisplayClass7_1 . <b__0> d.MoveNext( )---在抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处App1.MainPage.d__7.MoveNext()中的.CompilerServices.TaskAwaiter.GetResult()---从抛出异常的上一个位置开始的堆栈跟踪---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)at位于App1.MainPage.d__9.MoveNext()的System.Runtime.CompilerServices.TaskAwaiter.GetResult()的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)System.UnauthorizedAccessException
我尝试用我的图片在C:/文件夹中创建一个新目录,更改安全权限,以管理员身份执行Visual Studio但没有改变...
var fs = new FileStream(@ path,FileMode.Open,FileAccess.Read,FileShare.Read)发生错误
我已经看过很多关于这一切的话题,我已经尝试了一切,但如果有人有任何想法,没有任何改变!
2 回答
不,不是的
StorageFile工作正常,但System.IO有一个System.UnauthorizedAccessException:'访问路径'C:\ 1 \ 1.txt'被拒绝 .
当用户使用
FileOpenPicker
选择任意文件时,您将获得StorageFile
实例 . 这是您可以访问该文件的唯一方法,您不能直接使用Path
,因为该API不在WinRT沙箱下 . 您只能使用Path
直接访问应用程序AppData
目录中的文件 .但是,您可以从检索到的
StorageFile
bu获取一个流,调用相应的扩展方法:更新
这篇文章中的信息现在已经过时了 . 自2018年4月更新Windows 10以来,您可以声明一个Broad Filesystem访问功能,该功能将允许您的应用访问文件系统中的任何路径,即使使用
System.IO
API也是如此 . 然而,在认证期间检查此功能,因此您必须确保应用程序有充分的理由可以访问所有用户的文件系统,因为您的应用程序将被拒绝,以防止恶意滥用 .