private void FileLoaded(LoadResult lr) { if (txtInput.InvokeRequired) { Action <LoadResult> act = FileLoaded; txtInput.BeginInvoke(act, lr); } else { if (lr.IsSuccess) { txtInput.Text = lr.Text; _regexExcuted = false; txtInput_SelectionChanged(txtInput, null); //SetLengthInfo(); } else { SetErrorInfo(lr.Error); } txtInput.Select(0, 0); if (OnFileLoaded != null) { OnFileLoaded(lr.FileOrUrl); } } }
private LoadResult OpenFileOrUrlInternal(string fileOrURL) { LoadResult result = null; string text; bool loadedCalled = false; try { if (Uri.IsWellFormedUriString(txtPathOrUrl.Text, UriKind.Absolute)) { loadedCalled = true; Func <string, Encoding, LoadResult> act = LoadPage; var enc = GetEncoding(); var res = act.BeginInvoke(fileOrURL, enc, (o) => { Func <string, Encoding, LoadResult> func = (Func <string, Encoding, LoadResult>)o.AsyncState; var val = func.EndInvoke(o); DataLoaded(val); }, act); //result = LoadPage(fileOrURL); } else { if (File.Exists(fileOrURL)) { text = File.ReadAllText(fileOrURL); ToolHelper.LastOpenedFile = fileOrURL; result = new LoadResult(fileOrURL, text); } else { result = new LoadResult(fileOrURL, "", false, string.Format("File '{0}' does not exist.", fileOrURL)); } } } catch (Exception ex) { result = new LoadResult(fileOrURL, "", false, ex.Message); } finally { if (false == loadedCalled) { DataLoaded(result); } } return(result); }
private void btnLoad_Click(object sender, EventArgs e) { //var btnLoadStr = btnLoad.Text; btnLoad.Enabled = false; LoadResult result = null; string text = string.Empty; string fileOrURL = string.Empty; if (string.IsNullOrWhiteSpace(txtPathOrUrl.Text)) { txtPathOrUrl.Text = string.Empty; var ofd = new OpenFileDialog(); ofd.FileName = ToolHelper.LastOpenedFile; if (ofd.ShowDialog() == DialogResult.OK) { fileOrURL = txtPathOrUrl.Text = ofd.FileName; } else { btnLoad.Enabled = true; return; } try { text = File.ReadAllText(fileOrURL); ToolHelper.LastOpenedFile = fileOrURL; result = new LoadResult(fileOrURL, text); } catch (Exception ex) { result = new LoadResult(fileOrURL, "", false, ex.Message); } DataLoaded(result); } else { fileOrURL = txtPathOrUrl.Text; result = OpenFileOrUrlInternal(fileOrURL); } }
private void DataLoaded(LoadResult result) { if (btnLoad.InvokeRequired) { Action <LoadResult> act = DataLoaded; btnLoad.BeginInvoke(act, result); } else { if (OnFileLoaded != null) { OnFileLoaded(result); } btnLoad.Enabled = true; } }
private LoadResult LoadPage(string url, Encoding enc) { string text; LoadResult result; try { var wr = (HttpWebRequest)WebRequest.Create(url); var response = (HttpWebResponse)wr.GetResponse(); if (enc == null) { using (StreamReader sr = new StreamReader(response.GetResponseStream(), true)) { text = sr.ReadToEnd(); result = new LoadResult(url, text); } } else { using (StreamReader sr = new StreamReader(response.GetResponseStream(), enc)) { text = sr.ReadToEnd(); result = new LoadResult(url, text); } } response.Close(); } catch (Exception ex) { result = new LoadResult(url, string.Empty, false, ex.Message); } return(result); }