开发者

System.IO.FileInfo throwing access is denied exception to a local file

I created a sample Silverlight Web project

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

开发者_如何学Go
string fileName = "map.gif";
FileInfo fileInfo = new FileInfo(fileName);

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

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


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

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;

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.

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.


You can't use the filesystem in Silverlight outside of Isolated Storage


you need to give file access to the asp.net user

check this out: http://www.codeproject.com/KB/aspnet/Ahmed_Kader.aspx

Or use the special folder which asp.net provides for you ... APP_DATA

that should have the rights you need...


I am assuming you are trying to access a file in the local filesystem.

If so, you cannot access files like that. Silverlight does not have the access priveleges u expect. If you want to add a file to your Silverlight Application at runtime. You will need to have Silverlight 4, running Out of the Browser with Elevated priveleges. There are certain limitations to this too. You can only access files in Special Folders like My Documents, Pictures, Music etc. For more info about access files this way. You can look at John's tutorials on Silverlight 4 elevated priveleges in Channel 9 MSDN.

I would doubt your FileInfo usage too. Here is a sample code to get file data using a simple drag and drop feature.

private void list_Drop(object sender, DragEventArgs e)
{
  FileInfo[] files = (FileInfo[])e.Data.GetData(DataFormats.FileDrop);

  for(int i=0;i<files.Length;i++)
  textblock.Text += files[i].Name;
}

You can get the properties of the file such as "Name". You wil not hit any access denied errors. You cannot access properties like "DirectoryName", "FullName" etc. The reason being they are declared as SecurityCritical properties for Security reasons. The advantage of elevated permissions is that you can get to local file system (special folders) to access the FullName and DirectoryName properties without any exceptions.

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜