07
2014
06

Silverlight通过WebClient查找遍历ClientBin文件夹下所有文件

 

在 Silverlight 调用WebClient 

 


        private void Button_Click_1(object sender, RoutedEventArgs ee)
        {
            Uri uri = new Uri(string.Format("/DataHandler_Get.ashx"), UriKind.Relative);
            WebClient client = new WebClient();
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(webclient1_OpenReadCompleted);
            client.OpenReadAsync(uri);
        }

        void webclient1_OpenReadCompleted(object sender, System.Net.OpenReadCompletedEventArgs e)
        {
            //判断写入是否有异常
            if (e.Error != null)
            {
                //System.Windows.Browser.HtmlPage.Window.Alert(e.Error.Message.ToString());
            }
            else
            {
                Stream res = e.Result;
                StreamReader sr = new StreamReader(res);
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    ListBox1.Items.Add(line);
                }
            }
        }

 

 

 

在SL所在WEB项目中添加 DataHandler_Get.ashx 

代码如下:

 

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class DataHandler_Get : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            response.Clear();

            string Res = "";
            using (Stream inputStream = request.InputStream)
            {
                string folder = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ClientBin/Images"));
                if (!System.IO.Directory.Exists(folder))
                    System.IO.Directory.CreateDirectory(folder);

                DirectoryInfo dir = new DirectoryInfo(folder);

                //遍历所有文件
                foreach (FileInfo f in dir.GetFiles())
                {
                    Res+= f.Name+ Environment.NewLine;
                }
            }
            response.Write(Res);
            response.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

 

« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。