Skip to main content

Microsoft Silverlight

Answered Question How to access FIle names from any folder at runtime ???RSS Feed

(0)

arpit.arp
arpit.arp

Member

Member

37 points

59 Posts

How to access FIle names from any folder at runtime ???

Hello All,

I want to access names of the files from the any folder in windows. I have used C# with silverlight and  I tried to do following steps

page.xaml

<UserControl
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="YetAnotherCarousel.Page"
   
    >
    <!--Width="1024" Height="768"-->

    <Canvas x:Name="LayoutRoot" Background="Black" Width="1200" Height="800" >
    </Canvas>
</UserControl>

 

page.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace YetAnotherCarousel
{
    public partial class Page : UserControl
    {
        public Page()
        {
            // Required to initialize variables
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

private void CreateImage()
        {
            Carousel carousel = new Carousel();
            Image[] imgObj = new Image[100];
            int j = 1;

          string[] fileEntries = Directory.GetFiles("D:/YetAnotherCarousel/YetAnotherCarousel/imges"); Surprise  ERROR COMES HERE

           foreach (string fileName in fileEntries)
              {

                        Image imge = new Image();
                        imgObj[j] = imge;
                        imgObj[j].Source = new BitmapImage(new Uri(fileName, UriKind.Relative));
                        imgObj[j].Stretch = Stretch.Fill;
                        imgObj[j].Width = 400;
                        imgObj[j].Height = 300;
                        carousel.CreateItem(imgObj[j],fileName);    //  CreateItem is a separate function in Carousel.cs
                        j = j + 1;

                 } 

               this.LayoutRoot.Children.Add(carousel);

              }

               void Page_Loaded(object sender, RoutedEventArgs e)
              {

                          CreateImage();

              } 

}

 references    mscorlib , system, system.core, system.Windows, system.windows.Browser, system.windows.controls.Extended, Sytem.xml.

 Problem

So when I rebuild the solution it rebuilds successfully . But, when I run the program in browser it shows the error :::

Surprise ( error locator symbol) / 4th row of createImage function

    "MethodAccessException was unhandled by  User Code"

    "Attempt to access the method failed: System.IO.Directory.GetFiles(System.String)"

I have full access rights to my system. But, still I am unable to solve the problem.

Please help me. It really mkaing me mad. As I tried doing the same thing in normal c# and its running perfectly there but not running here in silverlight

 

 Disclaimer:

PLEASE EXCUSE ME , if I asked some very basic thing , as I am new to Silverlight and trying my best to get the hands on this technology.

 

arpitgupta1@in.com

Arpit  . Pune INDIA

 

Arpit Gupta
Pune,INDIA

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Answered Question

Re: How to access FIle names from any folder at runtime ???

Hi,

You are trying to access client system's Files.

Due to Security Reason you will not be able to acess that.

http://www.ddj.com/windows/208300036

Check this Article for details.

Mark as answer if this post answered your question.

Harsh Bardhan
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities