posted on August 6, 2009

不改程序, 创建同一个程序不同服务名的方法

以往写windows service都需要个ProjectInstaller和serviceInstaller并配置serviceName,这样build出来的exe在install成windows service时我们会:

@echo 安装WindowService
@Set Path=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;
@Set svn_dir=%cd%
installutil %svn_dir%\MyServiceDemo.exe
pause
@echo 成功!

但服务名被我们编译在程序里了,如果这个exe想被装很多次服务怎么办呢?
今天从同事Martin Jia那里学来一个好方法,可以不用ProjectInstaller、serviceInstaller这种东西,直接安装exe为windows service并在安装时指定服务名称

阅读剩余部分...

posted on July 21, 2009

Asp.Net发邮件,如何发送附件

使用如下代码可以发送附件

MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body.";
MailAttachment attachment = new MailAttachment( Server.MapPath( "mailTest.txt" ) ); //create the attachment
mail.Attachments.Add( attachment );
SmtpMail.SmtpServer = "localhost"; 
SmtpMail.Send( mail );

使用如下代码可以在用户上传附件后发生邮件:

阅读剩余部分...

posted on July 17, 2009

知识死角,name相同的html element

今天在帮同事调程序时发现了这个死角
现象如下,没有任何输入的textarea提交到服务端后得到的值总是","
找了半天发现是因为页面上两个textarea的name相同,以前只知道页面上radio的name相同则是一个组,name相同的
的checkbox提交后是以逗号分隔值的,于是做了下面代码的试验,发现任何name相同的element提交到服务端都会得到逗号分隔的值,以前还不知道这个,汗

阅读剩余部分...

posted on July 15, 2009

C#中如何深度克隆一个对象?

如何深度克隆一个对象?

普通版:

        public static object CloneObject( object obj )
        {
            using ( MemoryStream memStream = new MemoryStream( ) )
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter( null ,
                     new StreamingContext( StreamingContextStates.Clone ) );
                binaryFormatter.Serialize( memStream , obj );
                memStream.Seek( 0 , SeekOrigin.Begin );
                return binaryFormatter.Deserialize( memStream );
            }
        }

泛型版:

阅读剩余部分...

posted on September 18, 2004

撞墙吧 Array.IndexOf(xxx)

要在后台把CheckBoxList的某个ListItem 的selected给true了
遍历该CheckBoxList,写下了下面注释内的代码,死活不能选中我的第个CheckBox

            string bigClassId = ( ( DataRowView)e.Item.DataItem )["模块编号"].ToString();
            
string bigClassName =  ( ( DataRowView)e.Item.DataItem )["模块名称"].ToString();
            CheckBoxList chk_list_big 
= (CheckBoxList)e.Item.FindControl("chk_list_big"as CheckBoxList;
            
if(chk_list_big != null)
            
{                    
                chk_list_big.Items.Clear();
                ListItem item 
= new ListItem();
                item.Text 
= bigClassName;
                item.Value 
= bigClassId;
                chk_list_big.Items.Add(item);
                chk_list_big.Attributes[
"onclick"= String.Format("CheckAll('{0}')",chk_list_big.ClientID);
                
//将符合用户模块的ListItem选定    
    
                
for(int i=0; i<this.taxiUser.Modules.Length; i++)
                
{
                    
if( bigClassId == this.taxiUser.Modules[i] )
                    
{
                        chk_list_big.Items.FindByValue(
this.taxiUser.Modules[i]).Selected = true;
                    }

                }

                //真是见了鬼了!为什么用这样的方法不能选定该ListItem???

//                foreach(ListItem listItem in chk_list_big.Items)
//                {
//                    if(Array.IndexOf(this.taxiUser.Modules,listItem.Value) > 0)
//                    {
//                        listItem.Selected = true;
//                    }
//                }
半天不能醒悟就想着换道道,写了”该死”那行上面的几行代码把数组放倒来,(站着不行我就躺着来),发现可行,然后就开始发楞,命名原理一摸一样的啊,真是“见鬼”,对者这几行代码发了会愣,就决定还要狠狠的
if (Array.IndexOf(xxxxx) > 0) 用力
555555,撞的头都疼了.

posted on August 21, 2004

疑惑 关于 多条件判断语句 的写法

写法1
if ( 条件1 && 条件2)
{
      //代码
}
写法2
if ( 条件1 )
{
       if( 条件2 )
      {
            //代码
      }
}
我一直认为写法1要比写法2条理清晰,容易阅读.
不知其他人如何认为呢?
今天在微软的KB中阅读 使用 Visual Basic .NET 在采用基于表单身份验证的 ASP.NET 应用程序中实现基于角色的安全性 的时候
发现这样的写法

public void Application_AuthenticateRequest( Object src , EventArgs e )
{
if (!(HttpContext.Current.User == null))
   
{
if (HttpContext.Current.User.Identity.AuthenticationType == "Forms" )
      
{
System.Web.Security.FormsIdentity id;
id 
= (System.Web.Security.FormsIdentity)HttpContext.Current.User.Identity;
String[] myRoles 
= new String[2];
myRoles[
0= "Manager";
myRoles[
1= "Admin";
HttpContext.Current.User 
= new System.Security.Principal.GenericPrincipal(id,myRoles);
      }

   }

}



而且这则KB的VB版本也是这样的写的.
这样写有优越性?