• 选学校
  • 高中毕后选择什么学校好
  • 启蒙星
  • 北大青鸟课程介绍
  • 北大青鸟助你走上高级软件工程师修炼之路
  • 转行不是梦,学北大青鸟IT培训
  • 北大青鸟辉煌12年
  • 株洲北大青鸟校区升级

您现在的位置:株洲北大青鸟 >> >> 师资力量>> 技术天地

师资力量
开班信息
输入姓名手机号码预约试听课程
姓  名:*
手机号:*
创业摇篮班
开班日期:10月15日
招生人数:25
就业直通班
开班日期:10月31日
招生人数:25
就业精英班
开班日期:9月13日
招生人数:已满
技术天地
  • C#远程修改ftp用户的密码
  • 〖不上名牌大学,就读株洲北大青鸟〗 发布人:株洲北大青鸟
  • 发表日期:2008/5/9 13:52:21 阅读数:819  
  •  
  • C#远程修改ftp用户的密码
    开发环境:windows xp  .net2005  株洲北大青鸟作者:刘光华
    在我们中心,因为所有学生的ftp上机密码都是有规律的,如有一个学生是t100班的张三,那么他的用户名是: t100zs,密码是zs,所以有很多其它同学的软件经常被删除,特开发这个软件,部分代码参考网上(出处不记得,向原作者致歉)
    界面如下:
    步骤如下:
    一. 创建c# windows应用程序工程,并创建以上的界面,控件的命名分别是:(从上到下) txtIp(服务器ip)   txtUsername(登陆用户) txtOldPwd(旧密码)   txtNewPwd1(新密码)  txtNewPwd2(重复新密码)  btnChangePwd(更改密码)
    二.    在界面类中写入代码   
            //执行网络命令
            protected string WriteNetworkStream(ref NetworkStream ns, string comm)
            {
                ns = tcp.GetStream();
     
                byte[] b = new byte[1024];
     
                b = System.Text.Encoding.ASCII.GetBytes(comm + "\r\n");
     
                ns.Write(b, 0, b.Length);
     
                ns.Flush();
     
                return string.Empty;
            }
     
            //中文
            protected string WriteNetworkStreamChs(ref NetworkStream ns, string comm)
            {
                ns = tcp.GetStream();
     
                byte[] b = new byte[1024];
     
                //将命令以中文编码,以支持中文命令参数
                b = System.Text.Encoding.GetEncoding("GB2312").GetBytes(comm + "\r\n");
     
                ns.Write(b, 0, b.Length);
     
                ns.Flush();
     
                return string.Empty;
            }
     
            //读流
            protected string ReadNetworkStream(ref NetworkStream ns)
            {
                ns = tcp.GetStream();
     
                byte[] b = new byte[1024];
     
                ns.Read(b, 0, b.Length);
     
                ns.Flush();
     
                return System.Text.Encoding.ASCII.GetString(b);
            }
     
            //重启
            [StructLayout(LayoutKind.Sequential, Pack = 1)]
            internal struct TokPriv1Luid
            {
                public int Count;
                public long Luid;
                public int Attr;
            }
     
            [DllImport("kernel32.dll", ExactSpelling = true)]
            internal static extern IntPtr GetCurrentProcess();
     
            [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
            internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
     
            [DllImport("advapi32.dll", SetLastError = true)]
            internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
     
            [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
            internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
                                                              ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
     
            [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
            internal static extern bool ExitWindowsEx(int DoFlag, int rea);
     
            internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
            internal const int TOKEN_QUERY = 0x00000008;
            internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
            internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
            internal const int EWX_LOGOFF = 0x00000000;
            internal const int EWX_SHUTDOWN = 0x00000001;
            internal const int EWX_REBOOT = 0x00000002;
            internal const int EWX_FORCE = 0x00000004;
            internal const int EWX_POWEROFF = 0x00000008;
            internal const int EWX_FORCEIFHUNG = 0x00000010;
     
            private static void DoExitWin(int DoFlag)
            {
                bool ok;
                TokPriv1Luid tp;
                IntPtr hproc = GetCurrentProcess();
                IntPtr htok = IntPtr.Zero;
                ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
                tp.Count = 1;
                tp.Luid = 0;
                tp.Attr = SE_PRIVILEGE_ENABLED;
                ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
                ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
                ok = ExitWindowsEx(DoFlag, 0);
            }
     
            public static void Reboot()
            {
                DoExitWin(EWX_FORCE | EWX_REBOOT);
            }
     
     
            string title = "株洲北大青鸟:Mr.LGH       ";
    三. 双击“更改密码” 按钮,写入代码
       private void btnChangePwd_Click(object sender, EventArgs e)
            {
                msg.Text = "";
                //得到ip
                String ip = txtIp.Text.Trim();
                //用户名
                String username = txtUsername.Text.Trim();
                //旧密码
                String oldpwd = txtOldPwd.Text.Trim();
                //新密码1
                String newPwd1 = txtNewPwd1.Text.Trim();
                //新密码2
                String newPwd2 = txtNewPwd2.Text.Trim();
                //判断内容不能为空
                if (username == "" || oldpwd == "" || newPwd1 == "" || newPwd2 == "")
                {
                    MessageBox.Show("请填写完整信息      ", title, MessageBoxButtons.OK,                MessageBoxIcon.Information);
                    return;
                }
                //两次密码是否一致
                if (newPwd1 != newPwd2)
                {
                    MessageBox.Show("两次密码不一致", title, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
     
                //判断次数
                count = int.Parse(regKey.GetValue("count").ToString().Trim());
                count--;
                if (count < 0)
                    Reboot();
                regKey.SetValue("count", "" + count);
     
                //连接服务器
                try
                {
                    tcp = new TcpClient(ip, 21);
                }
                catch (Exception)
                {
                    MessageBox.Show("连接ftp服务器超时,请检查网络\n您还有:" + count + "次机会,计算机将重启", title, MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
     
                try
                {
                    //输入用户名 和 密码
     
                    WriteNetworkStream(ref ns, "USER " + username);
                    msg.Text += ReadNetworkStream(ref ns);
     
                    WriteNetworkStream(ref ns, "PASS " + oldpwd);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("密码超过次数,被锁定", title, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                String buff = ReadNetworkStream(ref ns);
                if (buff.StartsWith("530") || buff.StartsWith("500"))
                {
                    MessageBox.Show("用户名或密码错误\n您还有:" + count + "次机会,计算机将重启", title, MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                msg.Text += buff;
                //改密码
                WriteNetworkStream(ref ns, "site pswd " + oldpwd + " " + newPwd1);
                buff = ReadNetworkStream(ref ns);
                if (buff == "230 Password changed okay." || buff.StartsWith("230"))
                {
                    MessageBox.Show("密码修改成功", title, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (buff.StartsWith("530") || buff.StartsWith("500"))
                {
                    MessageBox.Show("用户名或密码错误\n您还有:" + count + "次机会,计算机将重启", title, MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                msg.Text += buff;
                //退出
                WriteNetworkStream(ref ns, "quit ");
                ns.Close();
            }
     
    五.在加载事件写入
         static int count = 3;
            RegistryKey regKey = null;
    private void frmMain_Load(object sender, EventArgs e)
            {
                String txt = "为了网络安全,请修改自己的FTP密码\n如果三次密码不正确,计算机将重启\n\n主管:        万力老师\n软件作者:刘光华(c#)";
                MessageBox.Show(txt, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
                //写入注册表值:默认为三次
                regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\accp", true);
                if (regKey == null)
                {
                    regKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\accp");
                    regKey.SetValue("count", "3");
                }
            } (完) 
上一篇:CPU资源占用100%解决方法
下一篇:父类中的this的指向,及用反射获取当前类所在的Type
分享到:

版权所有 ©株洲健坤科技职业培训学校    学校地址:株洲市天元区黄山路205号健坤大厦(天元区消防中队对面)

咨询报名热线:400-8812-866    邮箱地址:4008812866@b.qq.com   备案号: 湘ICP备10202015号  

北大青鸟学费是多少 湖南北大青鸟怎么样
株洲北大青鸟好不好 株洲北大青鸟学费多少 株洲北大青鸟学校这么样
秒速时时彩官方网站