关于HashTable的应用(二)

using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;
using  System.IO;
using  System.Text;

namespace  HashTest
{
    
/// <summary>
    
/// HashPhone 的摘要说明。
    
/// </summary>

    public class HashPhone : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Label Label1;
        
protected System.Web.UI.WebControls.TextBox Reg_Phone;
        
protected System.Web.UI.WebControls.Label Label2;
        
protected System.Web.UI.WebControls.TextBox Intro_Phone;
        
protected System.Web.UI.WebControls.LinkButton SubClear;
        
        
private static string txtFilePath = @"E:HashTestFetion.txt";
        
        
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
        
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
        
        
protected static Hashtable hs = new Hashtable();
        
protected static Hashtable hs_time = new Hashtable();

        
protected System.Web.UI.WebControls.Label Label3;
        
protected System.Web.UI.WebControls.ImageButton ImageButton1;
        
protected System.Web.UI.WebControls.ImageButton ImageButton2;
        
protected static System.DateTime dt = System.DateTime.Now;
        Hashtable C_hash  
=new Hashtable();
        System.Text.StringBuilder sb 
=new StringBuilder();
        
static System.DateTime lastwritetime = System.DateTime.Parse("1900-1-1 00:00:00");
        
static StreamWriter fileWrite = null;

        
static HashPhone()
        
{
            hs.Clear();
            hs_time.Clear();
            StreamReader fileStream 
= new StreamReader(txtFilePath,Encoding.Default);
            
for(string content = "";(content = fileStream.ReadLine()) != null;)
            
{
                
try
                
{
                    
int regpos = content.IndexOf("注册手机号:");
                    
int regtimepos = content.IndexOf("注册时间:");
                    
string key = content.Substring(regpos + 6,regtimepos - regpos - 6);
                    
string number = content.Substring(7,regpos - 7);
                    System.DateTime times 
= System.DateTime.Parse(content.Substring(regtimepos + 5));
                    
//推荐人手机号:13772037984注册手机号:15009297363注册时间:2008-1-3 13:16:20
                    hs.Add(key,number); 
                    hs_time.Add(key,times);
                }

                
catch
                
{
                }

            }

            fileStream.Close();
        }


        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            
//this.Streamread();
            
        }
 

        
Web 窗体设计器生成的代码

        
private static void Streamwrite(string s)
        
{
            fileWrite 
= new StreamWriter(txtFilePath,false,Encoding.Default);
            fileWrite.Write(s.ToString()
+" ");
            fileWrite.Flush();
            fileWrite.Close();
        }

        
        
private void Streamread()
        
{
            StreamReader fileStream 
= new StreamReader(txtFilePath,Encoding.Default);
            Response.Write(fileStream.ReadToEnd());
            fileStream.Close();
        }


        
private void SubClear_Click(object sender, System.EventArgs e)
        
{
            
this.Intro_Phone.Text="";
            
this.Reg_Phone.Text = "";
            hs.Clear();
            hs_time.Clear();
            C_hash.Clear();
            Response.Write(
"<script>alert('清除成功')</script>");
        }


        
private void Add(string rp,string ip)
        
{
            hs.Add(rp,ip);
        }


        
private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        
{
            Response.Write(
"<script>window.opener=null;window.close();</script>");
        }


        
private void ImageButton2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        
{
            
string key = this.Reg_Phone.Text;
            key 
= key.Replace(" ","").Replace(" ","").Trim();
            
string[] skey =key.TrimEnd(';').Split(';');
            
for(int i=0;i<skey.Length;i++)
            
{
                
if(skey[i]!=null && skey[i]!="" && skey[i]!=this.Intro_Phone.Text & !hs.Contains(skey[i]))
                
{
                    hs.Add(skey[i],
this.Intro_Phone.Text); 
                    hs_time.Add(skey[i],System.DateTime.Now);
                }

            }


            System.TimeSpan ts 
= System.DateTime.Now - lastwritetime;
            
if(ts.TotalSeconds >= 60)
                timer1_Elapsed();

            Response.Write(
"<script>alert('提交成功')</script>");
                
        }


        
private void timer1_Elapsed()
        
{
            C_hash 
= new Hashtable();
            lastwritetime 
= System.DateTime.Now;
            System.Collections.Hashtable C_hash_time 
= new Hashtable();
            
lock(this)
            
{
                
foreach(DictionaryEntry d in hs)   
                
{   
                    
string str_key  = d.Key.ToString();   
                    
string str_value = d.Value.ToString();   
                    
                    C_hash.Add(str_key,str_value);   
                    C_hash_time.Add(str_key,hs_time[str_key]);
                }
   

            }


            
foreach(DictionaryEntry de in C_hash)
            
{
                sb.Append(
"推荐人手机号:");
                sb.Append(de.Value.ToString());
                sb.Append(
"注册手机号:");
                sb.Append(de.Key.ToString());
                sb.Append(
"注册时间:");
                sb.Append(C_hash_time[de.Key]);
                sb.Append(
" ");
            }

            
            Streamwrite(sb.ToString());
        }



    }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C#中,Hashtable是一种经典的集合类型,它提供了键值对的存储和访问。以下是使用Hashtable的示例代码: ```csharp using System; using System.Collections; class Program { static void Main() { // 创建一个Hashtable Hashtable hashtable = new Hashtable(); // 添加键值对到Hashtable hashtable.Add("key1", "value1"); hashtable.Add("key2", "value2"); hashtable.Add("key3", "value3"); // 判断是否包含指定的键 bool containsKey = hashtable.ContainsKey("key2"); Console.WriteLine("Contains Key 'key2': " + containsKey); // 获取指定键对应的值 string value = (string)hashtable["key1"]; Console.WriteLine("Value for Key 'key1': " + value); // 修改指定键对应的值 hashtable["key3"] = "updated value"; // 移除指定键值对 hashtable.Remove("key2"); // 遍历Hashtable中的键值对 foreach (DictionaryEntry entry in hashtable) { Console.WriteLine(entry.Key + ": " + entry.Value); } } } ``` 上述代码中,首先创建了一个Hashtable对象。然后使用`Add`方法向Hashtable中添加键值对。可以使用`ContainsKey`方法判断Hashtable中是否包含指定的键。使用索引器(`[]`)来获取指定键对应的值,并可以通过索引器来修改指定键对应的值。`Remove`方法可以移除指定键值对。最后,通过`foreach`循环遍历Hashtable中的键值对,并输出到控制台。 请注意,Hashtable中的键和值可以是任意类型的对象。在实际应用中,请根据具体需求选择合适的数据结构和类型。另外,从C# 2.0开始,推荐使用更为类型安全的`Dictionary<TKey, TValue>`代替Hashtable
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值