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

孤者浪人 提交于 2019-12-11 07:32:59

问题


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);

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

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


回答1:


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.




回答2:


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




回答3:


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...




回答4:


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




回答5:


@Lucas..

Well. You can using Silverlight 4. Take a look at this. and this



来源:https://stackoverflow.com/questions/3479162/system-io-fileinfo-throwing-access-is-denied-exception-to-a-local-file

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