File Security


  • I'm having an issue where if I retrieve a FileInfo object from an OpenFileDialog I get security exceptions when I try to get any path information from it:

    {System.SystemException} = {System.Security.SecurityException: [FileSecurityState_OperationNotPermitted]

    I am running the application as a trusted oob application. Is there anything else I need to do?

    Wednesday, November 25, 2009 8:28 PM


All replies

  • The Silverlight OpenFileDialog has some limitations. From the MSDN:


    The ShowDialog method can only be called from user-initiated code, such as a button Click event. If ShowDialog is called from code that is not user-initiated, such as a Page Loaded event, a SecurityException is thrown.

    The Silverlight plug-in does not support OpenFileDialog in full-screen mode. In most cases, displaying this dialog box in full-screen mode will cause the plug-in to revert to embedded mode. To avoid issues on some browsers, you should exit full-screen mode before using this class. For more information, see Full-Screen Support.


    So the code has to be directly in the event handler for example.

    Thursday, November 26, 2009 2:27 AM
  • I am calling the dialog from the event handler. However, since I support opening files via a dialog and also from drag and drop I have a common LoadFiles method that takes a collection of file names.

    What I'm seeing is that the security exception gets thrown as I'm trying to access the FileInfo, before I even leave the event handler.



    1            void btnAddImage_Click(object sender, RoutedEventArgs e)
    2            {
    3                OpenFileDialog ofd = new OpenFileDialog();
    5                ofd.Filter = "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg";
    6                ofd.FilterIndex = 1;
    8                ofd.Multiselect = true;
    10               bool? userClickedOK = ofd.ShowDialog();
    12               if (userClickedOK == true)
    13               {
    14                   var fileNames = new List<string>();
    16                   foreach (var item in ofd.Files)
    17                   {
    18                       fileNames.Add(item.Directory + item.Name); //This is where it happens. Even the debugger can't see in here.    
    19                   }
    21                   LoadImages(fileNames);
    22               }
    24           }
    Thursday, November 26, 2009 10:43 AM
  • Hi,

    FileInfo.Directory is restricted, you cannot access it from silverlight.


    Monday, November 30, 2009 12:57 AM
  • so how do you get to the file system in OOB ?
    Tuesday, March 09, 2010 1:14 AM
  • If the supported SL-set of folders (Documents, Pictures, etc.) doesn't suffice, you can switch to Com Interop to access the file system without restrictions. A bit like they did here: .

    Just implement an IDispatch-Interface which helps you navigate and passes you file streams.

    Thursday, March 11, 2010 8:28 AM
  • Dear Mog; I love your little piggy ! But I think you are wrong. I have seen demos where people do these things from Silverlight. Out of Browser should give you elevated permissions. Only it does not. I'm looking at getting a certificate for my assembly. I would also like to probe my security envelope so I don't cause security exceptions, i will test if I have permissions before I attempt security restricted operations. So check please 1 DR. k (I still love you and your piggy)
    Tuesday, March 16, 2010 11:27 AM
  • Dear teichgraf; I love your demos and your love of graphics and math. We need more of that in the silverlight world. I expect to be studying your bitmap graphics soon ! but first... i need access to the local and server file system. OOB should give you regular permissions like any old com or console project. If I understand the docs correctly. I will test your thing. I will post some code for pushing/probing the security envelope. Cheers! dB
    Tuesday, March 16, 2010 11:29 AM