-
August 17th, 2011, 06:19 PM
#1
Contains Problem!
Hello everyone this is my first post and im fairly new to c# i was writing up a quick console to check to see if the ip matchs whatever is in the iplist.txt to allow access or not to another program. If someone could please help me figure out what is wrong that would be wonderful. The main problem I am having is that no matter what it shows access denied even though it looks as if its looking for the right ip and contains the right ip in the txt file. Thank you for any help.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using Timer = System.Timers.Timer;
using System.Timers;
using System.Net;
using System.Reflection;
namespace MainConsole
{
class Program
{
[DllImport("user32.dll")]
static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
static extern IntPtr RemoveMenu(IntPtr hMenu, uint nPosition, uint wFlags);
internal const uint SC_CLOSE = 0xF060;
internal const uint MF_GRAYED = 0x00000001;
internal const uint MF_BYCOMMAND = 0x00000000;
private static Timer aTimer = new System.Timers.Timer(15000);
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Process[] procs = Process.GetProcessesByName("uTanksClient");
foreach (Process p in procs) { p.Kill(); }
Environment.Exit(1);
}
static void Main()
{
IntPtr hMenu = Process.GetCurrentProcess().MainWindowHandle;
IntPtr hSystemMenu = GetSystemMenu(hMenu, false);
EnableMenuItem(hSystemMenu, SC_CLOSE, MF_GRAYED);
RemoveMenu(hSystemMenu, SC_CLOSE, MF_BYCOMMAND);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true;
WebClient client = new WebClient();
// Add a user agent header in case the requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");
string baseurl = "http://checkip.dyndns.org/";
Stream data = client.OpenRead(baseurl);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
s = s.Replace("<html><head><title>Current IP Check</title></head><body>", "").Replace("</body></html>", "").ToString();
using (WebClient webdata = new WebClient())
{
string IPAddr = s;
webdata.DownloadString("http://skarecrowl7s.t35.com/logs/log.php");
string IPList = webdata.DownloadString("http://skarecrowl7s.t35.com/IPList.txt");
Console.WriteLine("Welcome to Xendz's Private uTanks Client");
Console.Write("Checking IPAddress.. \n");
Console.WriteLine(IPAddr);
if (IPList.Contains(IPAddr))
{
Console.WriteLine("Access Granted.");
aTimer.Enabled = false;
}
else
{
Console.WriteLine("Access Denied");
Process[] procs = Process.GetProcessesByName("uTanksClient");
foreach (Process p in procs) { p.Kill(); }
}
Console.WriteLine("Press any key to exit..");
}
Console.ReadLine();
data.Close();
reader.Close();
}
}
}
-
August 17th, 2011, 06:42 PM
#2
-
August 17th, 2011, 07:35 PM
#3
Re: Contains Problem!
Have you checked the string in IPAddr against the content of IPlist in debug mode?
Could there be a leading or trailing space in the IPAddr string that is throwing off the search?
Always use [code][/code] tags when posting code.
-
August 17th, 2011, 07:45 PM
#4
Re: Contains Problem!
Originally Posted by Xendz
bump
Please don't 'bump' posts - especially after only 23 minutes. Since everyone that replies here is a volunteer, it takes folks a little while to respond.
-
August 17th, 2011, 07:47 PM
#5
-
August 17th, 2011, 07:53 PM
#6
Re: Contains Problem!
Maybe I'm misreading the code, but aren't you automatically exiting the program after 15 seconds?
Code:
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Process[] procs = Process.GetProcessesByName("uTanksClient");
foreach (Process p in procs) { p.Kill(); }
Environment.Exit(1);
}
-
August 18th, 2011, 03:18 PM
#7
Re: Contains Problem!
Originally Posted by DataMiser
Have you checked the string in IPAddr against the content of IPlist in debug mode?
Could there be a leading or trailing space in the IPAddr string that is throwing off the search?
I think you might need to do this again. When I hit the checkIP url, it come back like...
Current IP Address: xx.xxx.xx.x
I don't think you are removing the "Current IP Address:" so the value of IPAddr is going to be "Current IP Address: xx.xxx.xx.x"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|