Click here to Skip to main content
11,642,216 members (65,915 online)
Click here to Skip to main content

How to Get Full File Path in Silverlight

, 26 Jun 2013 CPOL 7.2K 82 3
Rate this:
Please Sign up or sign in to vote.
A workaround to SecurityException while getting file's full path in Silverlight

Introduction

Usually a Silverlight application is not allowed to access a FileInfo's full path attribute, even via reflection. This is because of the security policy.

But sometimes, for our applications, we need to store a file's path, especially if talking about network directories. To workaround this, we can use JavaScript! Honesty I hate JavaScript, but sometimes there is no other way. Silverlight is even too much secure for production applications! Smile | :)

The basic idea is to grant access to path by using JavaScript and then send back the value to Silverlight for our use.

Note

This solution will perfectly work on Internet Explorer, will only show file name on Firefox and won't work on Chrome, because of Input element implementation... :@
If you have any solution to this, please report it to me and I will update this tip page! Smile | :)

Background

Please refer to this article if you don't know how Silverlight/JavaScript communication works.

Using the Code

1. Edit HTML/ASPX Page

First let's add this script code to our head element:

<script type="text/javascript">
     var silverlightCtrl = null;
 
     function pluginLoaded(sender, args) {
         silverlightCtrl = sender.getHost();
     }
 
     function JS_GetFilePath() {
         document.getElementById("my_file_element").click();
     }
 
     function sendPathToSL() {
         var element = document.getElementById("my_file_element");
         silverlightCtrl.Content.SL2JS.SL_SetFilePath(element.value);
     }
</script>

Then this to the Silverlight object element:

<param name="onLoad" value="pluginLoaded" />  

And finally this on the bottom of the body element:

<form enctype="multipart/form-data" 
method="post" style="visibility:hidden; 
height:0px; width:0px; border:0px; overflow:hidden">
     <input id="my_file_element" type="file" 
     name="file_1" onchange="sendPathToSL();">
</form>  

2. Edit C# Class

What we need to do now is, in order:

  1. Register class for listen to JavaScript events
  2. Invoke the JS_GetFilePath method
  3. Wait for the answer
1. Register class as listener by adding this row in the constructor:
HtmlPage.RegisterScriptableObject("SL2JS", this); 
2. Invoke JavaScript method with:
System.Windows.Browser.HtmlPage.Window.Invoke("JS_GetFilePath");  
3. Wait for answer in the method:
 [ScriptableMember]
 public void SL_SetFilePath(string path)
 {
       Txt_Path.Text = path;
 } 

NOTE: Remember to mark this method with [ScriptableMember] tag!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ivan Signore
Software Developer Overit - Engineering group (Italy)
Italy Italy
.NET developer specialized in Silverlight and Esri-GIS technologies.

You may also be interested in...

Comments and Discussions

 
QuestionQuestion Pin
Frostmura23-Apr-15 6:30
memberFrostmura23-Apr-15 6:30 
AnswerRe: Question Pin
Ivan Signore8-Jun-15 6:35
memberIvan Signore8-Jun-15 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 27 Jun 2013
Article Copyright 2013 by Ivan Signore
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid