I'm posting today about the lack of posting (which will change Monday at the latest). I've had two weeks of vacation and I tried my best to sleep through it... Well that and I was kind of busy blowing up Megaton... That game is very addicting. I also ended up with a Zune to replace my iPod. I have to say that I love it thus far. I really was expecting to not like it (one of those gifts that you like where you say "Oh... It's great..."). Thus far it sounds about 10x better, seems a bit more durable (plus it hasn't hung at all unlike my iPod, although apparently the 30 gigs bricked this year), etc. Plus I really do like the radio built in as it should help at the gym (although making up the words myself on those tv programs is sometimes fun).
Anyway, I do have a little bit of code for you today. Nothing big, just some code to get a document from a web site:
WebClient Client = null;
StreamReader Reader = null;
string Contents="";
try
{
Client = new WebClient();
Reader = new StreamReader(Client.OpenRead(FileName));
Contents = Reader.ReadToEnd();
Reader.Close();
}
catch
{
}
finally
{
if (Reader != null)
{
Reader.Close();
}
}
That's all there is to it. Just create a web client and have that download the item, just using a stream to get the file contents. That's all there is to it. Anyway, hopefully Monday I'll have something more interesting for you. For now, happy belated new year and happy coding... Also, why isn't Felicia Day the sexiest geek of 2008? I don't get that list. Kari Byron, Stephen Colbert, Tina Fey, Jade Raymond, and Danica McKellar I understand to a certain extent. Being in Hitchhiker's Guide, playing WoW, or even acting in Buffy doesn't count as geek though. Also, I've never heard of Philip DeFranco or Marina Orlova before now (YouTube fame = no fame in my book). But I still say it's rigged if Day isn't even in the top ten...
Be the first to rate this post - Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
I try to stay up to date on about 10 or so languages (I'm currently looking through the potential updates to C++ and liking most of them). One of the languages I try to stay on top of is PHP and as such I subscribe to a couple of blogs on the subject (Although I find it difficult to find good ones on the subject. If someone has a list of decent ones, feel free to send it to me.) What I find funny is the fact that about once a month either they write or link to an article about why ASP.Net "sucks". Everyone has their opinion on various languages and even I have languages that I hate (Lisp), but I at least understand the need for them, try to understand their strengths/weaknesses, etc. These posts annoy me though as the vast majority of them have their facts wrong and are usually biased because of, well, Microsoft (which I would liken to my bashing of Google/Apple, except in my case I bash because I know the companies can do better. The posts I'm talking about just hate Microsoft.)
All of that being said, I figured I'd write a short list of the common misconceptions that these posts latch onto and why they're wrong:
-
Windows hosting costs more - This used to be true. I remember trying to find windows hosting early in 2000 and it would have cost me double the cost of a Linux host in some cases. However every host that I've used in the past four years or so has offered it at the same price as Linux hosting (and sometimes as little as $5 a month). Sometimes the feature set is different, but usually not to the point where it would make a difference. Plus if you don't want to use windows/IIS, you don't have to because of...
-
I have to use Windows Server/IIS - This hasn't been true for a while now. Mono is currently at 2.0 and while there are still issues between it and running code on Windows, their not that big of a deal most of the time. But if you want, you can run Mono on Apache on a Linux box with no issues (in fact, Ubuntu comes with Mono by default). Heck, if you want to run .Net projects on OS X, you can with Mono (although I haven't tried it yet to see how well it works).
-
I'm stuck using Visual Studio - Once again, not true. MonoDevelop can be used to create ASP.Net web sites on Linux.
-
I like OS X and you can't do ASP.Net on a Mac - You didn't bother going to the MonoDevelop page, did you? It works on OS X.
-
I can't make a standards compliant website using ASP.Net - The standard controls that come with ASP.Net aren't the best and for some odd reason people stop as soon as they see that fact. But if you don't like them, you don't have to use them. You can roll your own, use the innerHTML property, etc. You have 100% control over what it spits out.
-
The view state is so large/ASP.Net is a bandwidth hog - Once again, you have complete control over what it spits out. You don't have to use the view state (turn it off) or if you do you can compact it or not even send it and save it on the server. On top of that you can turn off "pretty printing", set up compression using gzip or deflate on the various pages, set ETags, etc.
I have more, but those are the ones I hear the most. Oh, that and "I don't want to be tied to MS". Once again, the top four items show that you don't have to touch a Microsoft product (I even have a test server running Linux, MySQL, Mono, etc. No MS products). I know that this wont change anyone's opinion, wont reach anyone who isn't already using .Net, etc. Personally, I like C# and ASP.Net, but I like PHP as well. I realize the strengths of both. I just wish that people would read up on something before dismissing it... Anyway, hopefully I'll have some more code by tomorrow. In the mean time, take a look at Mono, set up a Linux box, etc. and happy coding.
Be the first to rate this post - Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
I've been doing a bit of work with the Web Browser control in .Net. I have to say that while it's extremely easy to use, it has some flaws. One of the big flaws (at least to me) is the inability to clear the cache. There is no function or property that allows you to do it, which is really rather annoying. However there is a way to do it, that's rather simple actually:
/**
* Modified from code originally found here: http://support.microsoft.com/kb/326201
**/
#region Usings
using System;
using System.Runtime.InteropServices;
#endregion
namespace Utilities.WebBrowser
{
// Class for deleting the cache.
static class WebBrowserHelper
{
#region Definitions/DLL Imports
// For PInvoke: Contains information about an entry in the Internet cache
[StructLayout(LayoutKind.Explicit, Size = 80)]
public struct INTERNET_CACHE_ENTRY_INFOA
{
[FieldOffset(0)]
public uint dwStructSize;
[FieldOffset(4)]
public IntPtr lpszSourceUrlName;
[FieldOffset(8)]
public IntPtr lpszLocalFileName;
[FieldOffset(12)]
public uint CacheEntryType;
[FieldOffset(16)]
public uint dwUseCount;
[FieldOffset(20)]
public uint dwHitRate;
[FieldOffset(24)]
public uint dwSizeLow;
[FieldOffset(28)]
public uint dwSizeHigh;
[FieldOffset(32)]
public FILETIME LastModifiedTime;
[FieldOffset(40)]
public FILETIME ExpireTime;
[FieldOffset(48)]
public FILETIME LastAccessTime;
[FieldOffset(56)]
public FILETIME LastSyncTime;
[FieldOffset(64)]
public IntPtr lpHeaderInfo;
[FieldOffset(68)]
public uint dwHeaderInfoSize;
[FieldOffset(72)]
public IntPtr lpszFileExtension;
[FieldOffset(76)]
public uint dwReserved;
[FieldOffset(76)]
public uint dwExemptDelta;
}
// For PInvoke: Initiates the enumeration of the cache groups in the Internet cache
[DllImport(@"wininet",
SetLastError = true,
CharSet = CharSet.Auto,
EntryPoint = "FindFirstUrlCacheGroup",
CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr FindFirstUrlCacheGroup(
int dwFlags,
int dwFilter,
IntPtr lpSearchCondition,
int dwSearchCondition,
ref long lpGroupId,
IntPtr lpReserved);
// For PInvoke: Retrieves the next cache group in a cache group enumeration
[DllImport(@"wininet",
SetLastError = true,
CharSet = CharSet.Auto,
EntryPoint = "FindNextUrlCacheGroup",
CallingConvention = CallingConvention.StdCall)]
public static extern bool FindNextUrlCacheGroup(
IntPtr hFind,
ref long lpGroupId,
IntPtr lpReserved);
// For PInvoke: Releases the specified GROUPID and any associated state in the cache index file
[DllImport(@"wininet",
SetLastError = true,
CharSet = CharSet.Auto,
EntryPoint = "DeleteUrlCacheGroup",
CallingConvention = CallingConvention.StdCall)]
public static extern bool DeleteUrlCacheGroup(
long GroupId,
int dwFlags,
IntPtr lpReserved);
// For PInvoke: Begins the enumeration of the Internet cache
[DllImport(@"wininet",
SetLastError = true,
CharSet = CharSet.Auto,
EntryPoint = "FindFirstUrlCacheEntryA",
CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr FindFirstUrlCacheEntry(
[MarshalAs(UnmanagedType.LPTStr)] string lpszUrlSearchPattern,
IntPtr lpFirstCacheEntryInfo,
ref int lpdwFirstCacheEntryInfoBufferSize);
// For PInvoke: Retrieves the next entry in the Internet cache
[DllImport(@"wininet",
SetLastError = true,
CharSet = CharSet.Auto,
EntryPoint = "FindNextUrlCacheEntryA",
CallingConvention = CallingConvention.StdCall)]
public static extern bool FindNextUrlCacheEntry(
IntPtr hFind,
IntPtr lpNextCacheEntryInfo,
ref int lpdwNextCacheEntryInfoBufferSize);
// For PInvoke: Removes the file that is associated with the source name from the cache, if the file exists
[DllImport(@"wininet",
SetLastError = true,
CharSet = CharSet.Auto,
EntryPoint = "DeleteUrlCacheEntryA",
CallingConvention = CallingConvention.StdCall)]
public static extern bool DeleteUrlCacheEntry(
IntPtr lpszUrlName);
#endregion
#region Public Static Functions
/// <summary>
/// Clears the cache of the web browser
/// </summary>
public static void ClearCache()
{
// Indicates that all of the cache groups in the user's system should be enumerated
const int CACHEGROUP_SEARCH_ALL = 0x0;
// Indicates that all the cache entries that are associated with the cache group
// should be deleted, unless the entry belongs to another cache group.
const int CACHEGROUP_FLAG_FLUSHURL_ONDELETE = 0x2;
// File not found.
const int ERROR_FILE_NOT_FOUND = 0x2;
// No more items have been found.
const int ERROR_NO_MORE_ITEMS = 259;
// Pointer to a GROUPID variable
long groupId = 0;
// Local variables
int cacheEntryInfoBufferSizeInitial = 0;
int cacheEntryInfoBufferSize = 0;
IntPtr cacheEntryInfoBuffer = IntPtr.Zero;
INTERNET_CACHE_ENTRY_INFOA internetCacheEntry;
IntPtr enumHandle = IntPtr.Zero;
bool returnValue = false;
// Delete the groups first.
// Groups may not always exist on the system.
// For more information, visit the following Microsoft Web site:
// http://msdn.microsoft.com/library/?url=/workshop/networking/wininet/overview/cache.asp
// By default, a URL does not belong to any group. Therefore, that cache may become
// empty even when the CacheGroup APIs are not used because the existing URL does not belong to any group.
enumHandle = FindFirstUrlCacheGroup(0, CACHEGROUP_SEARCH_ALL, IntPtr.Zero, 0, ref groupId, IntPtr.Zero);
// If there are no items in the Cache, you are finished.
if (enumHandle != IntPtr.Zero && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
return;
// Loop through Cache Group, and then delete entries.
while (true)
{
if (ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error() || ERROR_FILE_NOT_FOUND == Marshal.GetLastWin32Error()) { break; }
// Delete a particular Cache Group.
returnValue = DeleteUrlCacheGroup(groupId, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, IntPtr.Zero);
if (!returnValue && ERROR_FILE_NOT_FOUND == Marshal.GetLastWin32Error())
{
returnValue = FindNextUrlCacheGroup(enumHandle, ref groupId, IntPtr.Zero);
}
if (!returnValue && (ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error() || ERROR_FILE_NOT_FOUND == Marshal.GetLastWin32Error()))
break;
}
// Start to delete URLs that do not belong to any group.
enumHandle = FindFirstUrlCacheEntry(null, IntPtr.Zero, ref cacheEntryInfoBufferSizeInitial);
if (enumHandle != IntPtr.Zero && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
return;
cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize);
enumHandle = FindFirstUrlCacheEntry(null, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
while (true)
{
internetCacheEntry = (INTERNET_CACHE_ENTRY_INFOA)Marshal.PtrToStructure(cacheEntryInfoBuffer, typeof(INTERNET_CACHE_ENTRY_INFOA));
if (ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error()) { break; }
cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize;
returnValue = DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName);
if (!returnValue)
{
returnValue = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
}
if (!returnValue && ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error())
{
break;
}
if (!returnValue && cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize)
{
cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial;
cacheEntryInfoBuffer = Marshal.ReAllocHGlobal(cacheEntryInfoBuffer, (IntPtr)cacheEntryInfoBufferSize);
returnValue = FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, ref cacheEntryInfoBufferSizeInitial);
}
}
Marshal.FreeHGlobal(cacheEntryInfoBuffer);
}
#endregion
}
}
That class right there is from the Help & Support section of MS's site and it was a pain to find. It does have a couple of modifications, but they're rather small and deal only with the case of having the cache empty already. Other than that though, there is really no change. All you need to do is call the ClearCache function and it does the rest. Anyway, I hope that this helps someone out. So try out the code, leave feedback (and probably send a thank you letter to Microsoft, because I never would have figured this one out on my own), and happy coding.
Oh, and before I forget. Left 4 Dead kicks ass... That is all...
Be the first to rate this post - Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
|
|