//______________________________________________________________________________________________________________________________ /// <summary> /// Limits a length of the passed argument to the number of specified characters starting from the beginning. /// </summary> /// <param name="length">A length that the given text should have.</param> /// <param name="text">A text data to limit.</param> /// <returns>A substring from the beginning to the specified number of length.</returns> public static string limitCharactersToFirst(int length, string text) { string qualifiedText = string.Empty; try { int textLength = text.Length; qualifiedText = text.Substring(0, (length > textLength - 1) ? (textLength) : (length)); } catch (ArgumentOutOfRangeException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = length.GetType().FullName + "~" + nameof(length); lastExceptionInfo.argValue = length.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-5]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (Exception x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = length.GetType().FullName + "~" + nameof(length); lastExceptionInfo.argValue = length.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-5]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } return(qualifiedText); }
//______________________________________________________________________________________________________________________________ /// <summary> /// Tries to retrieve the application location (a path with the application file name) at runtime. /// </summary> /// <returns>The application full path or an empty string if any error occurs.</returns> private static string gainApplicationLocation() { string appPath = string.Empty; try { appPath = typeof(Program).Assembly.Location; } catch (NotSupportedException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = appPath.GetType().FullName + "~" + nameof(appPath); lastExceptionInfo.argValue = appPath.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-1]"; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ")"); StdErrFlow.writeLine(Environment.NewLine); } catch (Exception x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = appPath.GetType().FullName + "~" + nameof(appPath); lastExceptionInfo.argValue = appPath.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-1]"; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ")"); StdErrFlow.writeLine(Environment.NewLine); } return(appPath); }
//______________________________________________________________________________________________________________________________ /// <summary> /// Creates the root directory using a directory name defined in a constant ROOT_DIRECTORY_NAME. /// Root directory means creating a directory in the location of executable program file location. /// </summary> /// <returns>A path of the root directory on successful directory creation, 'null' otherwise</returns> public static string createRootDirectory() { string path = null; string rootDirectoryName = string.Empty; try { rootDirectoryName = Path.GetDirectoryName(getApplicationFullPath()); string rootDirectory = Path.Combine(rootDirectoryName, ROOT_DIRECTORY_NAME); if (createDirectory(rootDirectory) == true) { path = rootDirectory; } } catch (ArgumentNullException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = rootDirectoryName.GetType().FullName + "~" + nameof(rootDirectoryName); lastExceptionInfo.argValue = rootDirectoryName.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-6]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (ArgumentException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = rootDirectoryName.GetType().FullName + "~" + nameof(rootDirectoryName); lastExceptionInfo.argValue = rootDirectoryName.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-6]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (PathTooLongException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = rootDirectoryName.GetType().FullName + "~" + nameof(rootDirectoryName); lastExceptionInfo.argValue = rootDirectoryName.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-6]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (Exception x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = rootDirectoryName.GetType().FullName + "~" + nameof(rootDirectoryName); lastExceptionInfo.argValue = rootDirectoryName.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-6]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } return(path); }
//______________________________________________________________________________________________________________________________ /// <summary> /// Downloads the HTML content from a website given as an URL using synchronous method. /// </summary> /// <param name="url">An absolute URL to connect with for downloading the website content.</param> /// <returns>The website content behind the passed URL.</returns> private string downloadWebsiteContent(string url) { string websiteContent = string.Empty; try { websiteContent = new System.Net.WebClient().DownloadString(url); } catch (ArgumentNullException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-2]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (NotSupportedException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-2]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (System.Net.WebException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-2]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (Exception x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-2]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } return(websiteContent); }
//______________________________________________________________________________________________________________________________ /// <summary> /// Debug test code. /// </summary> static void Main() { StdErrFlow.tryToRedirectStdErr(); SiteCrawler sc = new SiteCrawler(); sc.setLevelOfDepth(2); sc.setSiteURL("http://www.a.pl"); sc.setAsynchronousDownloadUse(false); sc.setMaximumRunningTasksPerDepthEntry(5); try { sc.crawlThroughSite(); } catch (ArgumentNullException x) { System.Diagnostics.Debug.WriteLine(x.ToString()); } catch (Exception x) { System.Diagnostics.Debug.WriteLine(x.ToString()); } StdErrFlow.tryToRetrievePreviousStdErr(); StdErrFlow.closeStreams(); }
//______________________________________________________________________________________________________________________________ /// <summary> /// Determines a qualified path. /// Qualified means without reserved to the Windows file system characters and with the proper length. /// If a combined path would be too long, the website URL name will be trimmed from the left side. /// </summary> /// <param name="directoryName">A directory name of the target destination location.</param> /// <param name="websiteURL">An untrusted name.</param> /// <returns>The Windows operating system qualified directory path.</returns> private string determineQualifiedPath(string directoryName, string websiteURL) { string qualifiedLengthPath = string.Empty; string mainSiteDirectoryName = string.Empty; try { string websiteName = FileSystemFlow.removeWindowsFileSystemReservedCharacters(websiteURL.Substring(websiteURL.IndexOf('.') + 1)); websiteName = FileSystemFlow.limitCharactersToFirst(FileSystemFlow.WINDOWS_QUALIFIED_FILENAME_LENGTH - 1, websiteName); mainSiteDirectoryName = System.IO.Path.Combine(directoryName, websiteName); if (mainSiteDirectoryName.Length > (FileSystemFlow.WINDOWS_QUALIFIED_DIRECTORY_LENGTH - 1)) { int trimmingSize = mainSiteDirectoryName.Length - FileSystemFlow.WINDOWS_QUALIFIED_DIRECTORY_LENGTH; websiteName = websiteName.Substring(trimmingSize + 1); mainSiteDirectoryName = System.IO.Path.Combine(directoryName, websiteName); } qualifiedLengthPath = mainSiteDirectoryName; } catch (ArgumentNullException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = mainSiteDirectoryName.GetType().FullName + "~" + nameof(mainSiteDirectoryName); this.lastExceptionInfo.argValue = mainSiteDirectoryName.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-5]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (ArgumentOutOfRangeException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = mainSiteDirectoryName.GetType().FullName + "~" + nameof(mainSiteDirectoryName); this.lastExceptionInfo.argValue = mainSiteDirectoryName.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-5]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (ArgumentException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = mainSiteDirectoryName.GetType().FullName + "~" + nameof(mainSiteDirectoryName); this.lastExceptionInfo.argValue = mainSiteDirectoryName.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-5]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (Exception x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = mainSiteDirectoryName.GetType().FullName + "~" + nameof(mainSiteDirectoryName); this.lastExceptionInfo.argValue = mainSiteDirectoryName.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-5]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } return(qualifiedLengthPath); }
//______________________________________________________________________________________________________________________________ /// <summary> /// Downloads the HTML content from a website given as an URL using asynchronous method. /// </summary> /// <param name="url">An absolute URL to connect with for downloading the website content.</param> /// <returns>The website content behind the passed URL.</returns> private string downloadWebsiteContentAsynchronously(string url) { string websiteContent = string.Empty; try { Uri uri = new Uri(url); System.Net.WebClient client = new System.Net.WebClient(); bool isDownloadFinished = false; System.Reflection.TargetInvocationException downloadException = null; client.DownloadStringCompleted += delegate(object sender, System.Net.DownloadStringCompletedEventArgs e) { // Preventing an internal exception raising. if ((e.Cancelled == false) && (e.Error == null)) { websiteContent = e.Result; isDownloadFinished = true; } else { downloadException = new System.Reflection.TargetInvocationException(e.Error.Message, e.Error); } }; client.DownloadStringAsync(uri); while (isDownloadFinished == false) { if (downloadException != null) { throw (downloadException); } } } catch (ArgumentNullException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (UriFormatException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (System.Net.WebException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (System.Reflection.TargetInvocationException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (Exception x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } return(websiteContent); }
//______________________________________________________________________________________________________________________________ /// <summary> /// Performs the recursive crawling. /// </summary> /// <param name="absoluteLinks0">A set of absolute links from the crawling-starting URL website content.</param> /// <param name="rootDirectoryName">The root directory full path.</param> /// <param name="levelOfDepthEntry">The current depth of the recursive entry.</param> private void performLevelCrawlingStep(ISet <string> absoluteLinks0, string rootDirectoryName, uint levelOfDepthEntry = 0) { if (levelOfDepthEntry > this.getLevelOfDepth()) { return; } if (levelOfDepthEntry == this.getLevelOfDepth()) { this.NewSetOfLinksFounded?.Invoke(this, new SiteCrawlerEventArgs(absoluteLinks0.Count)); } try { // foreach ( var urlEntry in absoluteLinks0 ) { System.Threading.Tasks.Parallel.ForEach( absoluteLinks0, new System.Threading.Tasks.ParallelOptions { MaxDegreeOfParallelism = this.getMaximumRunningTasksPerDepthEntry() }, urlEntry => { string directoryName = System.IO.Path.Combine(rootDirectoryName, "lvl_" + levelOfDepthEntry.ToString()); ISet <string> absoluteLinks1 = this.performBasicCrawlingStep(urlEntry, directoryName); this.performLevelCrawlingStep(absoluteLinks1, rootDirectoryName, levelOfDepthEntry + 1); } ); } catch (ArgumentNullException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = absoluteLinks0.GetType().Name + ".Count"; this.lastExceptionInfo.argValue = absoluteLinks0.Count.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (ArgumentOutOfRangeException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = absoluteLinks0.GetType().Name + ".Count"; this.lastExceptionInfo.argValue = absoluteLinks0.Count.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (ArgumentException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = absoluteLinks0.GetType().Name + ".Count"; this.lastExceptionInfo.argValue = absoluteLinks0.Count.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (AggregateException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = absoluteLinks0.GetType().Name + ".Count"; this.lastExceptionInfo.argValue = absoluteLinks0.Count.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (OperationCanceledException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = absoluteLinks0.GetType().Name + ".Count"; this.lastExceptionInfo.argValue = absoluteLinks0.Count.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (ObjectDisposedException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = absoluteLinks0.GetType().Name + ".Count"; this.lastExceptionInfo.argValue = absoluteLinks0.Count.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (Exception x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = absoluteLinks0.GetType().Name + ".Count"; this.lastExceptionInfo.argValue = absoluteLinks0.Count.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } }
//______________________________________________________________________________________________________________________________ /// <summary> /// Probing the network connection with the object's given URL. This method works in a synchronous way. /// On success, the site content will be saved to the proper object's field. /// </summary> /// <returns>'true' if connection has been successfully established, 'false' otherwise.</returns> public bool probeNetworkConnection() { string url = this.getSiteURL(); try { if (url == string.Empty) { throw (new ArgumentException("The site URL is an empty string.")); } string content = new System.Net.WebClient().DownloadString(url); this.probedSiteContent = content; } catch (ArgumentNullException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-1]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (ArgumentException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-1]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (System.Net.WebException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-1]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (NotSupportedException x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-1]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (Exception x) { this.lastExceptionInfo.typeName = x.GetType().ToString(); this.lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; this.lastExceptionInfo.argName = url.GetType().FullName + "~" + nameof(url); this.lastExceptionInfo.argValue = url.ToString(); this.lastExceptionInfo.message = x.Message; this.lastExceptionInfo.id = "[SC-1]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } return(true); }
//______________________________________________________________________________________________________________________________ /// <summary> /// Removes all of the reserved in the Windows file system characters. /// Please note that this method do not removes the reserved special file names, i.e. CON, COM0, LPT0, AUX, NUL, PRN etc. /// </summary> /// <param name="path">A file system path to check.</param> /// <returns>The passed argument without Windows illegal paths and filenames characters.</returns> public static string removeWindowsFileSystemReservedCharacters(string path) { string validWindowsPath = path; try { string regexSearch = new string( Path.GetInvalidFileNameChars()) + new string( Path.GetInvalidPathChars()); Regex regex = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch))); validWindowsPath = regex.Replace(path, ""); } catch (ArgumentNullException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-2]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (ArgumentException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-2]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (FormatException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-2]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (RegexMatchTimeoutException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-2]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } catch (Exception x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-2]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); } return(validWindowsPath); }
//______________________________________________________________________________________________________________________________ /// <summary> /// Save the given text data to the specified location with the specified file name into a file. /// </summary> /// <param name="filePath">A path with the file name.</param> /// <param name="fileContent">A content to write into a file.</param> /// <returns>'true' if IO operations have been done successfully, 'false' otherwise.</returns> public static bool saveTextToFile(string filePath, string fileContent) { try { using (StreamWriter writer = File.CreateText(filePath)) { writer.Write(fileContent); } } catch (UnauthorizedAccessException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = filePath.GetType().FullName + "~" + nameof(filePath); lastExceptionInfo.argValue = filePath.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (ArgumentNullException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = filePath.GetType().FullName + "~" + nameof(filePath); lastExceptionInfo.argValue = filePath.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (ArgumentException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = filePath.GetType().FullName + "~" + nameof(filePath); lastExceptionInfo.argValue = filePath.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (PathTooLongException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = filePath.GetType().FullName + "~" + nameof(filePath); lastExceptionInfo.argValue = filePath.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (DirectoryNotFoundException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = filePath.GetType().FullName + "~" + nameof(filePath); lastExceptionInfo.argValue = filePath.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (NotSupportedException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = filePath.GetType().FullName + "~" + nameof(filePath); lastExceptionInfo.argValue = filePath.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (ObjectDisposedException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = filePath.GetType().FullName + "~" + nameof(filePath); lastExceptionInfo.argValue = filePath.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (IOException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = filePath.GetType().FullName + "~" + nameof(filePath); lastExceptionInfo.argValue = filePath.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (Exception x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = filePath.GetType().FullName + "~" + nameof(filePath); lastExceptionInfo.argValue = filePath.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-4]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } return(true); }
//______________________________________________________________________________________________________________________________ /// <summary> /// Creates all directories and subdirectories in the specified path unless they already exist. /// </summary> /// <param name="path">An absolute path containing the directory name to create.</param> /// <returns>'true' if the directory has been created, 'false' otherwise.</returns> public static bool createDirectory(string path) { try { Directory.CreateDirectory(path); } catch (DirectoryNotFoundException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (PathTooLongException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (IOException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (UnauthorizedAccessException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (ArgumentNullException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (ArgumentException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (NotSupportedException x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } catch (Exception x) { lastExceptionInfo.typeName = x.GetType().ToString(); lastExceptionInfo.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; lastExceptionInfo.argName = path.GetType().FullName + "~" + nameof(path); lastExceptionInfo.argValue = path.ToString(); lastExceptionInfo.message = x.Message; lastExceptionInfo.id = "[FSF-3]"; string args = lastExceptionInfo.argName + "=" + lastExceptionInfo.argValue; StdErrFlow.writeLine(lastExceptionInfo.id + " " + x.ToString() + " (" + lastExceptionInfo.methodName + ") " + args + Environment.NewLine); return(false); } return(true); }