Page view counter
How to access FIle names from any folder at runtime ???
Last post 10-07-2008 10:47 PM by HarshBardhan. 15 replies.
Sort Posts:
09-29-2008 3:41 AM
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 Gupta

Pune INDIA

 

arpit.arp

Loading...
Joined on 09-29-2008
Pune
Posts 14
09-29-2008 5:24 AM
Re: How to access FIle names from any folder at runtime ???

hi,

 I think it is a Duplicate post and there are 3-4 Posts discussing same thing.

http://silverlight.net/forums/t/31254.aspx

Please avoid Duplicate Posts.It is Difficult to follow.

Mark as answer if this post answered your question.

Harsh Bardhan

HarshBardhan

Loading...
Joined on 05-07-2008
Bangalore
Posts 1,161
09-29-2008 8:16 AM
Re: Re: How to access FIle names from any folder at runtime ???

 hi,

   thanks for the reply. But, my main appplication is that - I need to access the names of the images from a folder. Can I access that Folder using silverlight with C# ??  I am trying to make a carousel in silverlight.

arpit.arp

Loading...
Joined on 09-29-2008
Pune
Posts 14
09-29-2008 8:17 AM
Re: Re: How to access FIle names from any folder at runtime ???

 hi,

   thanks for the reply. But, my main appplication is that - I need to access the names of the images from a folder. Can I access that Folder using silverlight with C# ??  I am trying to make a carousel in silverlight.

arpit.arp

Loading...
Joined on 09-29-2008
Pune
Posts 14
09-29-2008 8:38 AM
Re: Re: How to access FIle names from any folder at runtime ???

hi,

You Can Definately acess that.

Create a Folder inside your Project and try to acess that.

you can create Folder in silverlight or Asp Project and Can acess that.Even you can get Http Path and can acess that.

e.g- image.Source = new BitmapImage(new Uri("Images/1.jpg", UriKind.Relative));

//Here inside images folder i am having image named 1.jpg

 

Mark as answer if this post answered your question.

Harsh Bardhan

HarshBardhan

Loading...
Joined on 05-07-2008
Bangalore
Posts 1,161
09-29-2008 9:00 AM
Re: Re: Re: How to access FIle names from any folder at runtime ???

 hi,

   That is ok I have  already implemented this thing -

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

           string[] names = { "imges/hitler.JPG", "imges/gandhi.JPG", "imges/padit-nehru.JPG", "imges/PassionOfTheChrist.JPG", "imges/mtbaker.JPG", "imges/mtbaker2.JPG" };
           
         for (int j = 0; j < 6; j++)
         {
             imgObj[j].Source = new BitmapImage(new Uri(names[j], UriKind.Relative));
             Image imge = new Image();
             imgObj[j] = imge;
               
             string img = names[j];
             imgObj[j].Source = new BitmapImage( new Uri(names[j].ToString(),UriKind.Relative));
             imgObj[j].Source = new BitmapImage(new Uri(img, UriKind.Relative));
             imgObj[j].Stretch = Stretch.Fill;
             imgObj[j].Width = 400;
             imgObj[j].Height = 300;

             carousel.CreateItem(imgObj[j], names[j]);
          }
            this.LayoutRoot.Children.Add(carousel);
        }

 and in this the problem is that I need to include the name of every image in the Array. This makes the application very hard coded and not interactive.

And, I have to access the the images which are there in the folder from first to end . That is - if a folder today is having 5 images and tomorrow I add more images i.e; from 5-10 and then on 3rd day I deleted some images from the same i.e; 3 deleted , left = 7.

Then there I need to access the images (image name /location ) at runtime . 

So this is the problem .  Please help me please

 

arpit.arp

Loading...
Joined on 09-29-2008
Pune
Posts 14
09-29-2008 9:01 AM
Re: Re: Re: How to access FIle names from any folder at runtime ???

 hi,

   That is ok I have  already implemented this thing -

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

           string[] names = { "imges/hitler.JPG", "imges/gandhi.JPG", "imges/padit-nehru.JPG", "imges/PassionOfTheChrist.JPG", "imges/mtbaker.JPG", "imges/mtbaker2.JPG" };
           
         for (int j = 0; j < 6; j++)
         {
             imgObj[j].Source = new BitmapImage(new Uri(names[j], UriKind.Relative));
             Image imge = new Image();
             imgObj[j] = imge;
               
             string img = names[j];
             imgObj[j].Source = new BitmapImage( new Uri(names[j].ToString(),UriKind.Relative));
             imgObj[j].Source = new BitmapImage(new Uri(img, UriKind.Relative));
             imgObj[j].Stretch = Stretch.Fill;
             imgObj[j].Width = 400;
             imgObj[j].Height = 300;

             carousel.CreateItem(imgObj[j], names[j]);
          }
            this.LayoutRoot.Children.Add(carousel);
        }

 and in this the problem is that I need to include the name of every image in the Array. This makes the application very hard coded and not interactive.

And, I have to access the the images which are there in the folder from first to end . That is - if a folder today is having 5 images and tomorrow I add more images i.e; from 5-10 and then on 3rd day I deleted some images from the same i.e; 3 deleted , left = 7.

Then there I need to access the images (image name /location ) at runtime . 

So this is the problem .  Please help me please

Regards,

 Arpit , pune

arpit.arp

Loading...
Joined on 09-29-2008
Pune
Posts 14
09-29-2008 9:38 AM
Re: Re: Re: How to access FIle names from any folder at runtime ???

Hi Arpit,

That thing is Really a ProblemYou have to take care of that one.

1)If you dont want to Change your code again and again probably you can create a int variable.

2)Assign it a number of images like today 7 images are there then set it as 7.

3)Name your images as 0.jpg,1.jpg like this.

So you can make them while looping like i.toString()+".jpg";

But here your name format should be like 1.jg,2.jpg and if you are deleting some thing you have to take care that you don't delete which is in between your First and last images.. 

 

Mark as answer if this post answered your question.

Harsh Bardhan

HarshBardhan

Loading...
Joined on 05-07-2008
Bangalore
Posts 1,161
09-29-2008 9:49 AM
Re: Re: Re: Re: How to access FIle names from any folder at runtime ???
Hi, Well, I think you took the problem other way. The folder is lying on the server. Now the application will read the files lying in the folder on the server and render them on the client site on the Silverlight web application. The feasible solution I can visualize is we can read the value of the folder using a C# class. Then dumping the values in the database and then read the filename using httpServices calls. In that way, I will get the data in a xml file which can be used to render the data in my webapplication. Or else, Webservices and remote object are other method. Not sure about silverlight but that's the way I think we can solve this problem. Please suggest!! Thanks, Arpit(Pune)
arpit.arp

Loading...
Joined on 09-29-2008
Pune
Posts 14
09-29-2008 10:35 AM
Marked as Answer
Re: Re: Re: Re: How to access FIle names from any folder at runtime ???

Hi,

Yes i thought your folders are lying in your Silverlight Project .

As far as You are talking about c# in silverlight your client will execute code in c3 only.

You can try same thing by building uri in your c# code and acess images like this:
(check this class Application.Current.Host.Source in Silverlight you will get Path of your Server application Path)

build uri like this now in Page.Xaml.cs :"http://yourservername"+"Images/"+i.toString()+".jpg";

Mark as answer if this post answered your question.

Harsh Bardhan

HarshBardhan

Loading...
Joined on 05-07-2008
Bangalore
Posts 1,161
10-06-2008 2:45 AM
Re: Re: Re: Re: Re: How to access FIle names from any folder at runtime ???

 hi,

     Well, the problem is still unresolved. See, the actual picture is that - " I have to access the names of the images which is lying in some folder in any of the drive ( say c:, d: , e: ,etc) .And that folder is added as (add new item or folder ) in silverlight project. "

And currently here is only my computer on which I am making the project and on the same machine the folder is there. There is no two machines - server , client etc. 

    In other  words, I am my own server and own client.

 so, I am unable to do so?? as you already know that the trials I have done form my side are already posted in the earlier post. 

 

Arpit Pune

 

arpit.arp

Loading...
Joined on 09-29-2008
Pune
Posts 14
10-06-2008 5:02 AM
Re: Re: Re: Re: Re: How to access FIle names from any folder at runtime ???

Hi,

 As you have added your folder in silverlight Project i don't think there is any Issue in Acessing it using Absolute or Relative Path.

If you are Still facing issue Host your Sample Project some where. 

Mark as answer if this post answered your question.

Harsh Bardhan

HarshBardhan

Loading...
Joined on 05-07-2008
Bangalore
Posts 1,161
10-06-2008 7:21 AM
Re: Re: Re: Re: Re: Re: How to access FIle names from any folder at runtime ???

 hi,

    But I have not added the  folder in my silverlight project . See, I want to access that folder's content from where - ever it is situated by just giving the path of the folder.

 

please help

Arpit Pune

 

arpit.arp

Loading...
Joined on 09-29-2008
Pune
Posts 14
10-06-2008 8:06 AM
Re: Re: Re: Re: Re: Re: How to access FIle names from any folder at runtime ???

Hi,

If you have not added your folder in your silverlight Project and your folder you are not able to acess using http you will not be able to acess those files.
As you are trying to acess Client machine(Security Constraint).

Mark as answer if this post answered your question.

Harsh Bardhan

HarshBardhan

Loading...
Joined on 05-07-2008
Bangalore
Posts 1,161
10-07-2008 6:02 AM
Re: Re: Re: Re: Re: Re: Re: How to access FIle names from any folder at runtime ???

hi,

    That means I cant access the files in the folder until unless I include that folder in the respective project,Right ???  If so, I include the folder in the project , this is also obvious that manually I need to include each and every image I need in my project.

But, it means that - the whole application become hard coded and not User friendly { as the only person who knows how to add image ( add existing item ) in visual studio can run the application ) and as this is a carousel application in silverlight you know that- carousel means, picture library.

So, moral is that - silverlight does not  include a user friendly functionality , like the one my applcation needs.Right ?????

As, this is a official microsoft site on silverlighht - can I know that , is it is all because silverlight is still in Beta version and Microsoft will come out with such a solution .

as the same code of accessing the files from folder runs in a normal c# program .

Arpit ,pune

 

arpit.arp

Loading...
Joined on 09-29-2008
Pune
Posts 14
10-07-2008 10:47 PM
Re: Re: Re: Re: Re: Re: Re: How to access FIle names from any folder at runtime ???

arpit.arp:
That means I cant access the files in the folder until unless I include that folder in the respective project,Right ???  If so, I include the folder in the project , this is also obvious that manually I need to include each and every image I need in my project.

Hi Arpit,

You are not adding that Folder to Project and You Don't want to acess that on Http then you are trying to acess Client Machine's Path which is Security Risk.It is not the fact that you have to add that Image to your Project but as you are developing web application either that shouuld be embedded in your application or it should be available at some Path.You can not directly give c://kind of stuff.

arpit.arp:
So, moral is that - silverlight does not  include a user friendly functionality , like the one my applcation needs.Right ?????

I don't know what do you mean by user Friendly???If other technology are allowing you to access client's folder than i don't think they are secure at all.As you mentioned only guys who know how to add images can run that application. You can very well specify Image Path here but as i mentioned that Path can be http .You can not give c:// kind of Path.

 It is not at all due to the fact that Silverlight is in Beta version but it is due to security.

Mark as answer if this post answered your question.

Harsh Bardhan

HarshBardhan

Loading...
Joined on 05-07-2008
Bangalore
Posts 1,161
Microsoft Communities