コード例 #1
0
        /// <summary>
        /// Get the temporary HTML Link file pathname that should be created for the specified URL. Keeps trying until
        /// an non-existing file pathname is generated.
        /// </summary>
        /// <param name="targetFolder">The target folder where to save the link html file.</param>
        /// <param name="uri">An URL string.</param>
        /// <returns>The pathname of the temporary HTML Link file.</returns>
        private static string GetLinkHTMLFileLocation(string targetFolder, NamedUrl uri)
        {
            for (int i = 0; i < int.MaxValue; i++)
            {
                string indexPart = i == 0 ? string.Empty : " " + i.ToString(CultureInfo.InvariantCulture);

                string linkHTMLFile = Path.Combine(
                    targetFolder,
                    string.Format(CultureInfo.InvariantCulture, Properties.Resources.StringLinkFileName, uri.Name, indexPart));

                // Return only if the file does not already exist
                if (!File.Exists(linkHTMLFile))
                {
                    return(linkHTMLFile);
                }
            }

            return(string.Empty);
        }
コード例 #2
0
        /// <summary>
        /// Tries to extract the URL send to the program as text or URL file, through the command line or
        /// the Clipboard.
        /// </summary>
        /// <returns>The URL string, or an empty string if no valid URL in sent to the program.</returns>
        public static ReadOnlyCollection <NamedUrl> GetUrls()
        {
            List <NamedUrl> urls = new List <NamedUrl>();

            // First try: if we have at least one command line argument, maybe they are URL or WEBSITE files.
            string[] commandLineArgs = Environment.GetCommandLineArgs();
            if (commandLineArgs.Length >= 2)
            {
                for (int i = 1; i < commandLineArgs.Length; i++)
                {
                    string argument = commandLineArgs[i];
                    if (!argument[0].Equals('/'))
                    {
                        NamedUrl url = UrlGetter.GetUrlFromUrlFile(argument);
                        if (url != null)
                        {
                            urls.Add(url);
                        }
                    }
                }

                if (urls.Count > 0)
                {
                    return(urls.AsReadOnly());
                }
            }

            // Second try: determine whether the Clipboard contains one or more URL as text line(s).
            if (Clipboard.ContainsText())
            {
                // Retrieve the text data from the Clipboard in Text or UnicodeText format.
                string clipText = Clipboard.GetText();

                // Split in lines
                string[] potentialUrls = clipText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string potentialUrl in potentialUrls)
                {
                    // Add the named url only if it is valid
                    NamedUrl url = new NamedUrl(potentialUrl, string.Empty);
                    if (!string.IsNullOrEmpty(url.Name))
                    {
                        urls.Add(url);
                    }
                }

                return(urls.AsReadOnly());
            }

            // Third try: determine whether the Clipboard contains one or more URLs as a URL or WEBSITE files
            if (Clipboard.ContainsFileDropList())
            {
                // Retrieve the file names from the Clipboard.
                StringCollection files = Clipboard.GetFileDropList();

                if ((files != null) && (files.Count > 0))
                {
                    foreach (string file in files)
                    {
                        urls.Add(UrlGetter.GetUrlFromUrlFile(file));
                    }

                    return(urls.AsReadOnly());
                }
            }

            return(null);
        }