07
2014
06

Silverlight通过WebClient删除ClientBin文件夹下指定的文件

 

MainPage.xaml.cs 代码


 

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            string name = ListBox1.SelectedItem.ToString();
            Uri uri = new Uri(string.Format("/DataHandler_Del.ashx?filename={0}", name), UriKind.Relative);
            WebClient client = new WebClient();
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(webclient2_OpenReadCompleted);
            client.OpenReadAsync(uri);
        }

        void webclient2_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= sr.ReadToEnd();
                MessageBox.Show(line == "1" ? "删除成功" : " 删除失败");
            }
        }

 

 

DataHandler_Del.ashx

 

 

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

        public void ProcessRequest(HttpContext context)
        {
            string filename = context.Request.QueryString["filename"].ToString();

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

            string Res = "";
            using (Stream inputStream = request.InputStream)
            {
                string file = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ClientBin/Images/" + filename));

                try
                {
                    if (File.Exists(file))
                    {
                        File.Delete(file);
                        Res = "1";
                    }
                    else
                        Res = "0";
                }
                catch (Exception ex)
                {
                    Res = "0";
                }
            }
            response.Write(Res);
            response.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

« 上一篇下一篇 »

发表评论:

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