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

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

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

Related

Per File access Control on Drupal 7 Content

Is there a way to restrict content download "Restricting Anonymous Users From Downloading Files"
Right now, once the user is logged in and he is able to obtain the URL to a path, he can re-download it again even if he logs off.
We've tried
-Rules Module and Content_Access Module to no avail.
it only supports:
Basic Rules per node
There's no
Basic Rules per Content (i.e. videos)
You can control access to files via Drupal only if both conditions are met:
private mode is on (see /admin/config/media/file-system)
Download folder is outside web server access, i.e. file upload folder contents are not accessible from the web.
See hook hook_file_download() which is called for every private file.
Control access to private file downloads and specify HTTP headers.
This hook allows modules enforce permissions on file downloads when the private file download method is selected. Modules can also provide headers to specify information like the file's name or MIME type.

Lync 2010 Plugin - Silverlight WPF with elevated permissions

I'm developing a CWE (Conversation Extensibility Window) with WPF and Silverlight 4 on Visual Studio 2010, for Lync 2010.
My application reads a list of .xml from the root directory of the application and into a string array.
I've this code:
bool featureComplete = Application.Current.HasElevatedPermissions;
if (featureComplete)
{
List<string> files = new List<string>(Directory.EnumerateFiles(textBox1.Text, "*.*"));
mensajes.Content = files.Count;
}
else
{
mensajes.Content = "no trust";
}
In the event handler of a button event. With any path (my documents, root, application, etc) the application says "no trust".
I change the properties on the build to Out-of-browser settings and also check "require elevated trust" but nothing happens.
I tried and tried looking for an answer on google and msdn but I could not find a solution.
So there's a checklist or step list to make a trusted CWE on Lync2010 with silverlight? I forgot something?
Please remember: this is not a usual web application, its a lync 2010 app.
ALSO: I can do it with "debug mode" with special folders like this tutorial: http://www.silverlight.net/learn/graphics/file-and-local-data/local-file-access
and it works, but when I run it under lync 2010 it says "access denied" for that folder.
There is the code that works only on debug as an application, and not works like lync applet:
List<string> picsinfolder = new List<string>();
var pics = Directory.EnumerateFiles
(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
foreach (var item in pics)
{
picsinfolder.Add(item);
}
listBox1.ItemsSource = picsinfolder;
Thanks a lot in advance.
PD: Work-arounds that fix the issue can be accepted as an answer
PD2: No, signing the xap doesn't work.
The work around I'm using and works is open an XML, reading, copy into Isolated Storage, read it again from there. Everytime I need to change the file, I read it and copy again on Isolated Storage

Silverlight 4 FileInfo.OpenRead method is working on any folder without elevated permissions

I have a Silverlight 4 application running inside the browser without elevated permission and in it I have an upload files functionality section where an OpenFileDialog window appear and you can select the files you want to upload and save the files into the database.
The problem is that the application can actually Access files outside the user's profile folders which is not allowed by the silverlight security policy.
private Asset ReadAsset(FileInfo fileInfo)
{
byte[] fileBuffer;
using (FileStream fileStream = fileInfo.OpenRead()) //This line works from any location
{
using (BinaryReader binaryReader = new BinaryReader(fileStream))
{
fileBuffer = binaryReader.ReadBytes((int)fileStream.Length);
binaryReader.Close();
}
fileStream.Close();
}
DirectoryInfo di = fileInfo.Directory; //This line doesn't work
}
This actually READ the files no matter the location (I could even read a file on system32 folder) and I have no means to get "My Documents" or "Documents" folder because even.
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Is not working. So in short. I can read the bytes from any file on any location which silverlight apps running on browsers are not suppose to do.
Any help will be appreciated.
I think what you seem to be concerned about is that via OpenFileDialog any file can be read regardless of its location in the client file system.
This fine and normal. The OOB with trust restrictions only apply to unsolicited access. That is access to the file system without direct and explicit interaction by the user.
In the case of OpenFileDialog the user has explicit specified what file(s) to select and users are free to select any files they wish. This is true even for an standard inbrowser app.

Config/Settings for a Microsoft CRM 2011 Form Silverlight Control

Need an idea on best practise in finding a solution.
We are looking at developing Silverlight controls for CRM forms that will need to reference a common configuration file for data. It is a file that will need to be maintained periodically every once in a while. We don't want to be hardcoding values into the Silverlight control.
My question is... Where/What/How do we provide a config file for a Silverlight control?
I tried uploading a JS web resource that simply was a JSON array full of settings that I tried to access from the Silverlight control. All I got were permission errors when I used both the admin account and my domain account to do a Http get of the file and parse it. Can someone confirm that this could work if I manage to work through these annoying permission errors?
My next thought was having a CRM entity full of settings that the Silverlight control could make ODATA calls to in order to get its config data. I'm not 100% sold on the idea though.
Perhaps there is another way people have been using - if so - I'd love to see what you are doing. This could really prevent us from coming to a Silverlight enriched solution that we are after.
Thanks in advance
We go about this in two ways.
We have a configuration entity for settings that might be changed on a customer site by their administrator.
For other configuration data that is unlikely to be changed, we install an XML web resource. This method means we can store a lot of data without having to create and manage complex entities (or relationships if required). If set as an unmanaged/customizable web resource, then the text editor can be used to make changes, although remember that these changes must not break the XML schema/syntax.
This xml web resource can be retrieved in Silverlight using WebClient.DownloadStringAsync() as shown below.
private void GetXmlConfiguration(string resourceName)
{
var webClient = new WebClient();
webClient.DownloadStringCompleted += OnGetConfigurationXmlCompleted;
webClient.DownloadStringAsync(new Uri("../Data/" + resourceName, UriKind.Relative));
}
private void OnGetConfigurationXmlCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null && !string.IsNullOrEmpty(e.Result))
{
//use xml string here
}
}
We use the configuration entity method quite often and I think it works well.
You should be able to use your initial method as well... I know in a few places we've done some XML configuration in a web resource that we've retrieved in Silverlight, parsed, and done something with.

Is it possible to read files stored on a computer?

I want to access a file in computer(c:\test.bin) and to read it as byte array .Is it possible in Windows phone .
Thanks and Regards
vaysage
You cannot access a file via any standard file I/O apis.
You can run a web server on that computer, make the file available via http and include the appropriate client access policy file in the web site. You can then download the file via WebClient using OpenReadAsync.
If you would like to upload something as a project asset, then you should force it to be a "content" from the properties panel and then access it using :
Uri uriMyFile = new Uri("test.bin",UriKind.relative);
StreamReader sr = new StreamReader((Application.GetResourcesStream(
uriMyFile)).Stream);

Resources