views:

61

answers:

5

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

A: 

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

BioBuckyBall
A: 

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

server info
The question is more Silverlight than ASP related...
Oren
+1  A: 

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.

AnthonyWJones
Yes, the image was placed in the silverlight project but as an Embedded Resource (not sure how that happened), so after examing the xap it wasn't there so I changed it to Content and it showed up and when I used the StreamResourceInfo it worked. I didn't know that Gif wasn't supported as an image but I wasn't using it for image anyways - so it is not a big deal. Thanks!!!
VoodooChild
Ok I have another question on this: what happens when I have the image added to the SL project as a Resource (it doesn't end up in the XAP file), how can get the Uri and Stream then....
VoodooChild
A: 

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

Aswin Ramakrishnan
A: 

@Lucas..

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

Aswin Ramakrishnan