<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How to check remote file exists using C#</title>
	<atom:link href="http://www.dotnetthoughts.net/2009/10/14/how-to-check-remote-file-exists-using-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net/2009/10/14/how-to-check-remote-file-exists-using-c/</link>
	<description>a dotnet developer&#039;s technical blog</description>
	<lastBuildDate>Thu, 09 Feb 2012 09:26:30 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: SAM</title>
		<link>http://www.dotnetthoughts.net/2009/10/14/how-to-check-remote-file-exists-using-c/#comment-633</link>
		<dc:creator>SAM</dc:creator>
		<pubDate>Tue, 13 Dec 2011 10:04:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=493#comment-633</guid>
		<description>First, thanks for your post.  It really helped me.

You can imagine a use case where you have 100s or 1000s of URLs that you want to check.  For me it is 700+ image files, but it could be FAQs, product landing pages, or any other case where the URLs can be generated from a database query or file system script.

What I did was put all of my URLs into a text file (MyLinks.txt).  It looks like this:

http://foo.org/foo/Images/Normal/1598_1_CR.jpg
http://foo.org/foo//Images/Normal/2.jpg
http://foo.org/foo/Images/Normal/31796.2001.jpg
http://foo.org/foo/Images/Normal/4.2001.jpg
etc....

I built a console app that goes through each URL in the file then check it with the code in this blog.  The entire thing is below.  I am amazed that I couldn&#039;t find an example of this and I was going to make a CodeProject articlee out of it, but it seems more appropriate to put it here.  Thanks again.

Steve

PS, One thing that is really important to do is add &quot;response.Close()&quot; which I did below.  It is also a good idea to put in a request.Timeout value.  

using System;
using System.Net;
using System.IO;

namespace LinkChecker
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] m_urls = File.ReadAllLines(@&quot;C:\LinkChecker\MyLinks.txt&quot;);

            //string[] m_urls = new string[] { m_arraystring.ToString() };

            StreamWriter m_results = new StreamWriter(@&quot;C:\LinkChecker\LinkCheckerResults.txt&quot;, true);

            // Print length of each row
            for (int i = 0; i &lt; m_urls.Length; i++)
            {
                Console.WriteLine(&quot;Status of URL &quot; + m_urls[i].ToString() + &quot; is &quot; + RemoteFileExists(m_urls[i]));
                m_results.WriteLine(&quot;Status of URL &quot; + m_urls[i].ToString() + &quot; is &quot; + RemoteFileExists(m_urls[i]));
                m_results.Flush();
            }

        }


        ///
        /// Checks the file exists or not.
        ///
        /// The URL of the remote file.
        /// True : If the file exits, False if file not exists
        public static bool RemoteFileExists(string url)
        {
            try
            {
                //Creating the HttpWebRequest
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                //Setting the Request method HEAD, you can also use GET too.
                request.Method = &quot;HEAD&quot;;
                request.Timeout = 10000;
                //Getting the Web Response.
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                response.Close();
                //Returns TURE if the Status code == 200
                return (response.StatusCode == HttpStatusCode.OK);
                
          }
            catch
            {
                //Any exception will returns false.
                return false;
            }
        }

    }</description>
		<content:encoded><![CDATA[<p>First, thanks for your post.  It really helped me.</p>
<p>You can imagine a use case where you have 100s or 1000s of URLs that you want to check.  For me it is 700+ image files, but it could be FAQs, product landing pages, or any other case where the URLs can be generated from a database query or file system script.</p>
<p>What I did was put all of my URLs into a text file (MyLinks.txt).  It looks like this:</p>
<p><a href="http://foo.org/foo/Images/Normal/1598_1_CR.jpg" rel="nofollow">http://foo.org/foo/Images/Normal/1598_1_CR.jpg</a><br />
<a href="http://foo.org/foo//Images/Normal/2.jpg" rel="nofollow">http://foo.org/foo//Images/Normal/2.jpg</a><br />
<a href="http://foo.org/foo/Images/Normal/31796.2001.jpg" rel="nofollow">http://foo.org/foo/Images/Normal/31796.2001.jpg</a><br />
<a href="http://foo.org/foo/Images/Normal/4.2001.jpg" rel="nofollow">http://foo.org/foo/Images/Normal/4.2001.jpg</a><br />
etc&#8230;.</p>
<p>I built a console app that goes through each URL in the file then check it with the code in this blog.  The entire thing is below.  I am amazed that I couldn&#8217;t find an example of this and I was going to make a CodeProject articlee out of it, but it seems more appropriate to put it here.  Thanks again.</p>
<p>Steve</p>
<p>PS, One thing that is really important to do is add &#8220;response.Close()&#8221; which I did below.  It is also a good idea to put in a request.Timeout value.  </p>
<p>using System;<br />
using System.Net;<br />
using System.IO;</p>
<p>namespace LinkChecker<br />
{<br />
    class Program<br />
    {<br />
        static void Main(string[] args)<br />
        {</p>
<p>            string[] m_urls = File.ReadAllLines(@&#8221;C:\LinkChecker\MyLinks.txt&#8221;);</p>
<p>            //string[] m_urls = new string[] { m_arraystring.ToString() };</p>
<p>            StreamWriter m_results = new StreamWriter(@&#8221;C:\LinkChecker\LinkCheckerResults.txt&#8221;, true);</p>
<p>            // Print length of each row<br />
            for (int i = 0; i &lt; m_urls.Length; i++)<br />
            {<br />
                Console.WriteLine(&quot;Status of URL &quot; + m_urls[i].ToString() + &quot; is &quot; + RemoteFileExists(m_urls[i]));<br />
                m_results.WriteLine(&quot;Status of URL &quot; + m_urls[i].ToString() + &quot; is &quot; + RemoteFileExists(m_urls[i]));<br />
                m_results.Flush();<br />
            }</p>
<p>        }</p>
<p>        ///<br />
        /// Checks the file exists or not.<br />
        ///<br />
        /// The URL of the remote file.<br />
        /// True : If the file exits, False if file not exists<br />
        public static bool RemoteFileExists(string url)<br />
        {<br />
            try<br />
            {<br />
                //Creating the HttpWebRequest<br />
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;<br />
                //Setting the Request method HEAD, you can also use GET too.<br />
                request.Method = &quot;HEAD&quot;;<br />
                request.Timeout = 10000;<br />
                //Getting the Web Response.<br />
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;<br />
                response.Close();<br />
                //Returns TURE if the Status code == 200<br />
                return (response.StatusCode == HttpStatusCode.OK);</p>
<p>          }<br />
            catch<br />
            {<br />
                //Any exception will returns false.<br />
                return false;<br />
            }<br />
        }</p>
<p>    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anuraj P</title>
		<link>http://www.dotnetthoughts.net/2009/10/14/how-to-check-remote-file-exists-using-c/#comment-538</link>
		<dc:creator>Anuraj P</dc:creator>
		<pubDate>Tue, 23 Aug 2011 12:02:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=493#comment-538</guid>
		<description>No, you can&#039;t use File.Exists it will work on local file system only.</description>
		<content:encoded><![CDATA[<p>No, you can&#8217;t use File.Exists it will work on local file system only.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Utku Feruz</title>
		<link>http://www.dotnetthoughts.net/2009/10/14/how-to-check-remote-file-exists-using-c/#comment-537</link>
		<dc:creator>Utku Feruz</dc:creator>
		<pubDate>Mon, 22 Aug 2011 23:16:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=493#comment-537</guid>
		<description>This one really helped me a lot.I&#039;m just wondering Is it possible to use File.Exists() instead of web request? Seems like you can&#039;t reach the remote server with that.</description>
		<content:encoded><![CDATA[<p>This one really helped me a lot.I&#8217;m just wondering Is it possible to use File.Exists() instead of web request? Seems like you can&#8217;t reach the remote server with that.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

