System.IO.FileInfo抛出访问被拒绝的异常到本地文件 [英] System.IO.FileInfo throwing access is denied exception to a local file

查看:161
本文介绍了System.IO.FileInfo抛出访问被拒绝的异常到本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个示例Silverlight Web项目

I created a sample Silverlight Web project

当我这样做时,我会收到访问被拒绝"的信息:

and I am getting 'Access is denied' when I do this:

string fileName = "map.gif";
FileInfo fileInfo = new FileInfo(fileName);

如何授予Web项目对此文件夹/文件的访问权限?

How can I give the web project access to this folder/file?

我将图像添加到我的项目中,在这里确实画了一个空白....

I added the image into my project, really drawing a blank here....

推荐答案

您不能使用FileInfo对象访问放置在项目中的文件.而是创建一个Uri来访问它.

You don't access files you've placed in the project using the FileInfo object. Instead you create a Uri to access it.

从您的问题中不清楚您将文件放置在哪个项目中.如果您将文件放置在Silverlight项目中,则该文件应最终作为Xap中的内容显示.在这种情况下,您可以使用以下命令获取StreamResourceInfo:-

Its not clear from your question which project you've place the file in. If you have placed it in the Silverlight project then it ought to end up as content in the Xap. In which case you can acquire StreamResourceInfo for it using:-

StreamResourceInfo gifContentInfo = Application.GetResourceStream(new Uri("map.gif", UriKind.Relative));

现在您可以通过以下方式获取文件内容:-

Now you can get to the file content with:-

Stream gifStream = gifContentInfo.Stream;

另一方面,如果您将文件放置在Web项目中,则它将是网站中的标准静态文件.因此,您将需要进行典型的WebClient下载以获取它.

On the other hand if you have placed the file in the web project it will be a standard static file in the web site. Hence you will need to do the typical WebClient download to fetch it.

我认为您会遇到麻烦,因为它是一个Gif文件;您知道它们不支持作为图像.

I take it you are going to this trouble because its a Gif file; you are aware that they are not supported as an image.

这篇关于System.IO.FileInfo抛出访问被拒绝的异常到本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆