|
Whats the best most efficient way of checking whether a resource (image, page, document etc.) exists at a particular URL web address location. Eg. I have a url http://www.example.com/myImage.jpg and I want to check whether myImage.jpg actually exists.
At the moment I'm using the WebClient class like so...
WebClient client = new WebClient();<br />
<br />
try<br />
{<br />
if(client.Download("http://www.example.com/myImage.jpg").Length > 0)<br />
{<br />
}<br />
else<br />
{<br />
}<br />
}<br />
catch<br />
{<br />
}
...problem with this method though is that it downloads the entire file to check it's existence. I'm not interested in the file, just whether the server will return a 404 or not.
Cheers!
|
|
|
|
|
Include System.IO and System.Text namespace to your project
Then you would be able to access File class which has a method defined for checking the existence of files as shown below
if (File.Exists(@"//localhost/command.gif") == false)
{
Response.Redirect("FileNotFound.htm");
}
else
{
Response.Write("File Exists");
}
I tested this code on my localhost, it works fine. I think this code would access the client machines path for the file and not the server's, just verify and let me know.
-- modified at 9:37 Monday 25th June, 2007
|
|
|
|
|
gladiatron wrote: I tested this code on my localhost, it works fine. I think this code would access the client machines path for the file and not the server's, just verify and let me know.
Just tested it with IronPython:
>>> ex = File.Exists("http://www.codeproject.com/script/images/news_answer.gif")
>>> ex
False
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Your question is very vague..why do you need to write the code to check for file existence on some other web server ? Its a built in thing, if u request for a non-existent page, the logic on that server will automatically handle the request and show you an error page. However, if u wanna do this for your own server you can, using the below given code.
if (File.Exists(Server.MapPath("web.gif")))
{
Response.Write("File Exists");
}
else
{
Response.Redirect("FileNotFound.htm");
}
This code will map the virtual path of your server so if some client browser requests for a file that does not exist on your web server, he will get an error page other wise the page will load, now it depends on you how you customize the application for your clients.
|
|
|
|
|
Long story but we have a list of external URLs which point to images on another web server, which we need to display on our site via an img tag or asp:Image control. Thing is some of these URLS are incorrect and point to images that don't exist, and rather than display an empty image box with a red X, they want a replacement image instead. Basically, they want us to guard against their own bad data. I know it's additional overhead but it's what they want, I think I can optimise it by caching the state of the external image for a time. But File.Exists("http://www.example.com/image123.jpg"); should do the trick.
If you know of a more efficient way let me know. Cheers!
|
|
|
|
|
hi
my application is written in C# in .NET compact framework 2003.
i need to display data in tabular form with row headers instead of column headers.
any suggestions?
thanks
|
|
|
|
|
I have a text file say log file. i have to extract all the characters between "<" sign and ">" sign.
i.e. between <> and put it into the database and log file is of kind
Sun, 24 Jun 2007 21:49:49 GMT: Received request , from <111.1.11.11>:<1111> at domain <localhost abc.net="">
Sun, 24 Jun 2007 21:49:49 GMT: UserName <abc>, Proxy <localhost-80>, URN <http: abc="" abc.gif="">
Sun, 24 Jun 2007 21:49:49 GMT: Processing Proxy request
Sun, 24 Jun 2007 21:49:50 GMT: Sending Msg <111> from Proxy Reader <1111111111> for URL <http: abc.abc="">with Params <> <abc>
Sun, 24 Jun 2007 21:49:50 GMT: Named pipe <\\.\pipe\1111111111> started for request <http: abc.abc="">
Sun, 24 Jun 2007 21:49:50 GMT: Received request , from <111.1.11.11>:<1111> at domain <localhost abc.net="">
Sun, 24 Jun 2007 21:49:50 GMT: UserName <abc>, Proxy <localhost-80>, URN <http: abc="" abc.gif="">
Sun, 24 Jun 2007 21:49:50 GMT: Processing Proxy request
Sun, 24 Jun 2007 21:49:52 GMT: Received Msg <222> for URL <http: abc.abc="">
Sun, 24 Jun 2007 21:49:52 GMT: Status: 200 OK, Reminder: Server: Microsoft-IIS/5.1
X-Powered-By: ASP.NET
Date: Mon, 25 Jun 2007 04:48:33 GMT
Content-Type: text/html
Accept-Ranges: bytes
Last-Modified: Thu, 22 Mar 2007 05:29:17 GMT
Sun, 24 Jun 2007 21:49:52 GMT: Last chunck read from pipe \\.\pipe\1111111111 for <http: abc.abc="">: <963>
Sun, 24 Jun 2007 21:49:52 GMT: Closing pipe <\\.\pipe\1111111111> for url <http: abc.abc="">
and i want to parse this file and extract and put the information from <> sign into the database
i had followed following link for file parsing
and instead of putting the info in dataset i m putting it into database.
Now my problem is my log file is of above format and i have to parse all the info within <>
I m using C# 1.1 version
does anybody have any idea or suggestions?
Thanks in advance
|
|
|
|
|
This Regex will work fine, if your strings won't contain any < and >-signs:
<([^<>]+)>
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thanks for reply Greeeg !
But the regular expression is not completely correct!
I just want the info within <> and nothing else but the
regular expression u had given is showing the rest info also.
Do u have any idea to extract all values within <>
?????
|
|
|
|
|
That's what the paratheses are for. It's called a "group". You can usually access the first group with matches.Group[1].Value
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
When i just change the location of a form with an overridden show-methode and theange of the size form is hidden while i change the location, the size will be changed too
like
public void Show( IWin32Window owner, int positionX, int positionY ) {
if( this.Visible == false ) {
this.SetDesktopLocation( positionX, positionY );
this.Size = this.RestoreBounds.Size;
this.BringToFront();
this.Show( owner );
}
else {
this.Activate();
}
}
|
|
|
|
|
I'm building FTP application (Download, Upload, Delete ..etc) and actually I don't have access to FTP server to test my code. is there any way to simulate FTP server at my machine or any free access FTP servers for testing.
Thanks in Advance.
"Our deepest fear is not that we are inadequate. Our deepest fear is that we are powerful beyond measure" -Nelson Mandela-
|
|
|
|
|
Windows XP Pro and Windows Server 2003 both have the option to install an FTP server when installing IIS.
Start Menu >> Control Panel >> Add or Remove Programs >> Add/Remove Windows Components
|
|
|
|
|
There are a lot of free hosters on the net, and also there are many FTP Servers freeware that you can install on your workstation.
An example, a excellent freeware FTP Server is CesarFTP.
Besides, try to search "free hosting services" on the net, almost all of those provide FTP access.
Carmine,
Webmaster of: www.thetotalsite.it
My web blog: blog.thetotalsite.it
|
|
|
|
|
i want to make T9 dictionary in c#.how to start?????????
|
|
|
|
|
I would guess that you would need to have a dictionary made up of words and their associated numeric values. Then, you would scan the numeric values to see what words matched. Beyond this, you could use it to predict words by pulling all the words that start with the predictive value based on the key sequence so far.
Beyond this, of course, you will need a dictionary of words. I've given you an indication of what you would need to do code wise - it's up to you to get the dictionary.
|
|
|
|
|
Didn't you ask this yesterday, and then not respond when people asked questions designed to move you in the right direction ?
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
|
So, how many times are you going to ask this question, then ignore the answers and questions that you get in reply?
|
|
|
|
|
As many times as it takes to get the answer he wants
only two letters away from being an asset
|
|
|
|
|
until one of three things happen. Some sucker gives him the code on a silver platter. The partial credit date for his homework assignment is passed. He's fired for not being able to do his job.
--
You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
|
|
|
|
|
I need to implement object in-memory cache .
Each object id can be composed from several string, integer and decimal type values.
ContainsKey does not find existing key in this case.
How to force ContainsKey to compare object contents and find key?
Should i use binaryformatter to serialize CacheKey, overload CacheKey == operator or any other idea ?
To reproduce,
run the code.
Observed:
Key not found
Expected:
Key found
using System.Collections.Generic;<br />
<br />
class test {<br />
struct CacheKey {<br />
object Key;<br />
public CacheKey(object key) {<br />
Key = key;<br />
}<br />
}<br />
<br />
static Dictionary<CacheKey, object> Cache =<br />
new Dictionary<CacheKey, object>();<br />
<br />
static void Main() {<br />
CacheKey key = new CacheKey(new object[] { "test1", 1 });<br />
Cache.Add(key, "");<br />
CacheKey key2 = new CacheKey(new object[] { "test1", 1 });<br />
if (!Cache.ContainsKey(key2))<br />
System.Windows.Forms.MessageBox.Show("Key not found");<br />
else<br />
System.Windows.Forms.MessageBox.Show("Key found");<br />
}<br />
}
Andrus
|
|
|
|
|
To work properly anything you use as a key must impliment GetHashCode().
As far as I know Object[] doesn't impliment it.
Your best bet would probably be to override ToString() on CacheKey to produce a string made up of the supplied components. Then override GetHashCode() to return the GetHashCode() of the composite string.
You'd probably want to impliment the equality stuff too:
Guidelines for Overloading Equals() and Operator == (C# Programming Guide)[^]
|
|
|
|
|
originSH wrote: To work properly anything you use as a key must impliment GetHashCode().
Dictionary class does not use GetHashCode() at all!
Why I need to implement it ?
originSH wrote: Your best bet would probably be to override ToString() on CacheKey to produce a string made up of the supplied components
Can I use binary or xml serializer for this or shoult I create some loop to concatenate values ?
originSH wrote: You'd probably want to impliment the equality stuff too:
If I use string returned from ToString() as key, why to overload equals ?
In this case I can use string as distionary key and I think string comparison works OK .
Andrus
|
|
|
|
|
AndrusM wrote: Dictionary class does not use GetHashCode() at all!
Errm - not true. Anything that ultimately derives from object implements GetHashCode(). What was being suggested though, was that your object needs to override GetHashCode().
|
|
|
|