- this,
- this,
- this,
- and this
- by letting you know that I figured out how to "find myself" within a WinForms app:
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = Directory.GetCurrentDirectory();
}
}
}
When I ran the app from Visual Studio label1 ended up with this in it:
C:\Apps\WinFormsApp\WindowsFormsApplication\WindowsFormsApplication\bin\Debug
When I published the app and then ran it label1 ended up with this in it:
C:\Users\Thomas\AppData\Local\Apps\2.0\NZX70T3A.BEY\WN7LA04B.TR4\wind..tion_c42fbaf68a73d8f4_0001.0000_4abed88245962350
While I am doing "Where Am I?" stuff, I wrote a scrapper for http://www.ipchicken.com/ which has not changed significantly since Dustin Wells first showed it to me in 2005. If you call the one public method, you will get back your IP in a string. The code in black exists only to accomodate IPv6 IP addresses. I do not have one myself so I could not test the IPv6 stuff. I also do not know if ipchicken will handle IPv6s correctly either.
using System.IO;
using System.Net;
using System.Text;
namespace WhereAmI.Models
{
public static class PublicIp
{
public static string Get()
{
string[] scrappings = ScrapeService().Split(">".ToCharArray());
string ip = null;
foreach (string scrapping in scrappings)
{
if (scrapping.IndexOf(".") > -1)
{
string[] subscrappings = scrapping.Split(".".ToCharArray());
if (subscrappings.Length == 4 && ip == null)
{
foreach (char character in scrapping.Split("<".ToCharArray())[0].ToLower())
{
if (IsCharacterOfIp(character))
{
ip = ip + character;
}
}
}
}
}
return ip;
}
private static string ScrapeService()
{
string url = "http://www.ipchicken.com/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
StringBuilder scrappingSpool = new StringBuilder();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
int counter = 0;
byte[] buffer = new byte[1000000];
do
{
counter = stream.Read(buffer, 0, buffer.Length);
if (counter != 0)
{
string chunk = Encoding.ASCII.GetString(buffer, 0, counter);
scrappingSpool.Append(chunk);
}
} while (counter > 0);
}
}
return scrappingSpool.ToString();
}
private static bool IsCharacterOfIp(char character)
{
if (character == '0') return true;
if (character == '1') return true;
if (character == '2') return true;
if (character == '3') return true;
if (character == '4') return true;
if (character == '5') return true;
if (character == '6') return true;
if (character == '7') return true;
if (character == '8') return true;
if (character == '9') return true;
if (character == 'a') return true;
if (character == 'b') return true;
if (character == 'c') return true;
if (character == 'd') return true;
if (character == 'e') return true;
if (character == 'f') return true;
if (character == ':') return true;
if (character == '%') return true;
if (character == '.') return true;
return false;
}
}
}
No comments:
Post a Comment