locked
using OpenFileDialog to retrieve file path (directory) RRS feed

  • Question

  • Hi All - I need to write a Silverlight app that will run in browser only (not out of browser). I need to be able to allow the user to select a file, on a network share, and I need to be able to extract that location (full path e.g. \\fileshare\folder1\test.docx) and store that directory as a string.

    Ultimately this will be hosted in a MS CRM 4.0 application, however, my reading online so far suggests that what i am trying to do is not possible.

    Can anyone clarify this, and if possible, give me a work around?

    Many thanks for your time.

    Tuesday, September 18, 2012 9:29 AM

Answers

  • Hi, as bitdisaster has provided you an good solution (use ofd.File.DirectoryName) and it works, but I have two things that need your attention:

    1.  OpenFileDialog.InitialDirectory is only supported in Silverlight 5, it is not supported in Silverlight 4 and earlier;

    2.  There are some limitations of accessing file.  Generally we need enable running application out-of-browser and elevate trust. But since you said your application is run inside browser, you can refer to this to Enable Trusted Applications to Run Inside the Browser.

    Best Regards,

    Tuesday, September 25, 2012 5:16 AM
    Moderator
  • I just tested this and with this code you get the full UNC path where the file is located.
         OpenFileDialog ofd = new OpenFileDialog();
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                ofd.Filter = "WordFile|*.docx";
                if (ofd.ShowDialog() == true)
                {
                   string folderPath = ofd.File.DirectoryName;
                }

    Wednesday, September 19, 2012 8:29 PM

All replies

  • are you using the build-in OpenFileDialog?

    Tuesday, September 18, 2012 5:50 PM
  • Hi - yes, it's the .Net OpenFileDialog.

    It is Silverlight 4, in browser (not out of browser).

    Wednesday, September 19, 2012 4:45 AM
  • I just tested this and with this code you get the full UNC path where the file is located.
         OpenFileDialog ofd = new OpenFileDialog();
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                ofd.Filter = "WordFile|*.docx";
                if (ofd.ShowDialog() == true)
                {
                   string folderPath = ofd.File.DirectoryName;
                }

    Wednesday, September 19, 2012 8:29 PM
  • Hi, as bitdisaster has provided you an good solution (use ofd.File.DirectoryName) and it works, but I have two things that need your attention:

    1.  OpenFileDialog.InitialDirectory is only supported in Silverlight 5, it is not supported in Silverlight 4 and earlier;

    2.  There are some limitations of accessing file.  Generally we need enable running application out-of-browser and elevate trust. But since you said your application is run inside browser, you can refer to this to Enable Trusted Applications to Run Inside the Browser.

    Best Regards,

    Tuesday, September 25, 2012 5:16 AM
    Moderator