简体   繁体   中英

Silverlight How to read a directory locally

I am new to Silverlight and I am trying to do a directory listing of the contents of a directory. However when the first list of this code runs, it throws an exception:

The application itself runs inside of a Browser.

File operation not permitted. Access to path 'C:\\Program Files\\AppName' is denied.

I checked permissions and they are readable so I'm not sure why it's not working.

    DirectoryInfo di = new DirectoryInfo(@"C:\Program Files\AppName");

    try
    {
        if (di.Exists)
        {
            Console.WriteLine("That path exists already.");
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("The process failed: {0}", e.ToString());
    }
    finally { }

Silverlight doesn't let you just access any old directory you want. Silverlight 4 added the ability to access certain well-known paths under the user profile but only in out-of-browser elevated trust applications.

Silverlight is probably not the technology you want to use for this purpose. Look into WPF instead.

If you want to save/load files in silverlight and your app owns the files, you can use isolated storage.

silverlight isolated storage example

Generally, this is useful to store cross-session data locally, or store user settings. Like Josh's answer says, there is no solution to your problem if you need to access other app's files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM