CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2011
    Posts
    3

    Cool 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();
            }
        }
    }

  2. #2
    Join Date
    Aug 2011
    Posts
    3

    Re: Contains Problem!

    bump

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Contains Problem!

    Quote Originally Posted by Xendz View Post
    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.

  5. #5
    Join Date
    Aug 2011
    Posts
    3

    Re: Contains Problem!

    I have, and roger that.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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);
            }

  7. #7
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Contains Problem!

    Quote Originally Posted by DataMiser View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured