程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#同步、異步長途下載文件實例

C#同步、異步長途下載文件實例

編輯:C#入門知識

C#同步、異步長途下載文件實例。本站提示廣大學習愛好者:(C#同步、異步長途下載文件實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#同步、異步長途下載文件實例正文


1.func Contains(s, substr string) bool這個函數是查找某個字符能否在這個字符串中存在,存在前往true

import (
 "fmt"
 "strings"
)

func main() {
 fmt.Println(strings.Contains("widuu", "wi")) //true
 fmt.Println(strings.Contains("wi", "widuu")) //false
}


2.func ContainsAny(s, chars string) bool這個是查詢字符串中能否包括多個字符


import (
 "fmt"
 "strings"
)

func main() {
 fmt.Println(strings.ContainsAny("widuu", "w&d")) //true
}


3.func ContainsRune(s string, r rune) bool,這裡邊固然是字符串中能否包括rune類型,個中rune類型是utf8.RUneCountString可以完全表現全體Unicode字符的類型


import (
 "fmt"
 "strings"
)

func main() {
 fmt.Println(strings.ContainsRune("widuu", rune('w'))) //true
 fmt.Println(strings.ContainsRune("widuu", 20))        //fasle
}


4.func Count(s, sep string) int這個的感化就是輸入,在一段字符串中有若干婚配到的字符


import (
 "fmt"
 "strings"
)

func main() {
 fmt.Println(strings.Count("widuu", "uu")) //1
 fmt.Println(strings.Count("widuu", "u"))  //2
}

5.func Index(s, sep string) int 這個函數是查找字符串,然後前往以後的地位,輸出的都是string類型,然後int的地位信息

import (
 "fmt"
 "strings"
)

func main() {
 fmt.Println(strings.Index("widuu", "i")) //1
 fmt.Println(strings.Index("widuu", "u")) //3
}


6.func IndexAny(s, chars string) int 這個函數是一樣的查找,字符串第一次湧現的地位,假如不存在就前往-1


import (
 "fmt"
 "strings"
)

func main() {
 fmt.Println(strings.IndexAny("widuu", "u")) //3
}


7.func IndexByte(s string, c byte) int,這個函數功效照樣查找第一次粗線的地位,只不外此次C是byte類型的,查找到前往地位,找不到前往-1


import (
 "fmt"
 "strings"
)

func main() {
 fmt.Println(strings.IndexByte("hello xiaowei", 'x')) //6
}


8.func IndexRune(s string, r rune) int,照樣查找地位,只不外此次是rune類型的

import (
 "fmt"
 "strings"
)

func main() {
 fmt.Println(strings.IndexRune("widuu", rune('w'))) //0
}


9.func IndexFunc(s string, f func(rune) bool) int這個函數年夜家一看就曉得了,是經由過程類型的轉換來用函數查找地位,我們來代碼看下哈


import (
 "fmt"
 "strings"
)

func main() {
 fmt.Println(strings.IndexFunc("nihaoma", split)) //3
}

func split(r rune) bool {
 if r == 'a' {
  return true
 }
 return false
}


10.func LastIndex(s, sep string) int 看到這個年夜家能夠也明確了查找的是最初湧現的地位,正好跟index相反


import (
 "fmt"
 "strings"
)

func main() {
 fmt.Println(strings.LastIndex("widuu", "u")) // 4
}


11.func LastIndexAny(s, chars string) int這個跟indexAny正好相反,也是查找最初一個

import (
 "fmt"
 "strings"
)

func main() {
 fmt.Println(strings.LastIndexAny("widuu", "u")) // 4
}


    try
            {
                if (!e.Cancelled && e.Error == null)
                {

                    string dnDir = string.Empty;
                    string domainName = string.Empty;
                    string uri = uriString;

                    取得域名 [url]httpwww.sina.com[url]
                    Match match = Regex.Match(uri, @((http(s)))+[w-.]+[^]);, RegexOptions.IgnoreCase
                    domainName = match.Value;

                    取得域名最深層目次 [url]httpwww.sina.commail[url]
                    if (domainName.Equals(uri))
                        dnDir = domainName;
                    else
                        dnDir = uri.Substring(0, uri.LastIndexOf(''));

                    dnDir += '';

                    獲得數據
                    string pageData = Encoding.UTF8.GetString(e.Result);
                    Liststring urlList = new Liststring();

                    婚配全途徑
                    match = Regex.Match(pageData, @((http(s)))+((()+[w-.]+()))+[w-.]+.+( + ImageType + )); , RegexOptions.IgnoreCase
                    while (match.Success)
                    {
                        string item = match.Value;
                        短途徑處置
                        if (item.IndexOf(http) == -1 && item.IndexOf(https) == -1)
                            item = (item[0] == ''  domainName  dnDir) + item;

                        if (!urlList.Contains(item))
                        {
                            urlList.Add(item);
                            imgUrlList.Add(item);

                            及時顯示剖析成果
                            AddlbShowItem(item);

                            邊剖析邊下載
                            WebRequest hwr = WebRequest.Create(item);
                            hwr.BeginGetResponse(new AsyncCallback(AsyncDownLoad), hwr);
                            hwr.Timeout = 0x30D40;        默許 0x186a0 - 100000 0x30D40 - 200000
                            hwr.Method = POST;
                            hwr.C;
                            hwr.MaximumAutomaticRedirections = 3;
                            hwr.Accept =imagegif, imagex-xbitmap, imagejpeg, imagepjpeg, applicationx-shockwave-flash, applicationvnd.ms-excel, applicationvnd.ms-powerpoint, applicationmsword, ;
                            hwr.Accept = imagegif, imagex-xbitmap, imagejpeg, imagepjpeg, ;
                            IAsyncResult iar = hwr.BeginGetResponse(new AsyncCallback(AsyncDownLoad), hwr);
                            iar.AsyncWaitHandle.WaitOne();
                        }
                        match = match.NextMatch();
                    }
                }
            }
            finally
            {
                waiter.Set();

                #region 剖析計時停止

                QueryPerformanceCounter(ref count1);
                count = count1 - count;
                result = (double)(count)  (double)freq;

                toolStripStatusLabel1.Text = 剖析終了!;
                toolStripStatusLabel2.Text = string.Format(  剖析耗時{0}秒, result);
                Application.DoEvents();

                #endregion

                剖析終了
                isAnalyzeComplete = true;
            }
        }

        /// <summary>
        /// 異步接收數據
        /// </summary>
        /// <param name="asyncResult"></param>
        public  void AsyncDownLoad(IAsyncResult asyncResult) 
        {
            #region 下載計時開端

            if (cfreq == 0)
            {
                QueryPerformanceFrequency(ref cfreq);
                QueryPerformanceCounter(ref ccount);
            }

            #endregion

            WebRequest request = (WebRequest)asyncResult.AsyncState;
            string url = request.RequestUri.ToString();
            try
            {
                WebResponse response = request.EndGetResponse(asyncResult);
                using (Stream stream = response.GetResponseStream())
                {
                    Image img = Image.FromStream(stream);
                    string[] tmpUrl = url.Split('.');
                    img.Save(string.Concat(savePath, "/", DateTime.Now.ToString("yyyyMMddHHmmssfff"), ".", tmpUrl[tmpUrl.Length - 1]));
                    img.Dispose();
                    stream.Close();
                }
                allDone.Set();

                //從未下載的列表中刪除曾經下載的圖片
                imgUrlList.Remove(url);

                //更新列表框
                int indexItem = this.lbShow.Items.IndexOf(url);
                if (indexItem >= 0 && indexItem <= this.lbShow.Items.Count)
                    SetlbShowItem(indexItem);
            }
            catch (Exception)
            {
                imgUrlList.Remove(url);
            }
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved