月度归档:2013 年五月

解决hyper-v配置centos5.6无法上网的问题

win2008里,装centos5.6。刚开始用旧的网络适配,可以访问外网,但外网不能访问内网。

以为是没装linux integrate service的原因,后来装了lisv2.1,也是不行,

在windows上使用ipconfig /all看到“本地网络 – 虚拟网络”的ipv4地址竟然是一个外网ip,不知道是什么原因,后来尝试给虚拟网络指定ip后问题解决。

另外:centos5.x用lisv2.x, centos6.x才能用lisv3.x

(转)C# Enum,Int,String的互相转换 枚举转换

Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型。如果没有显式声明基础类型,则使用 Int32。编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举。

注意:枚举类型的基类型是除 Char 外的任何整型,所以枚举类型的值是整型值。

Enum 提供一些实用的静态方法:

(1)比较枚举类的实例的方法

(2)将实例的值转换为其字符串表示形式的方法

(3)将数字的字符串表示形式转换为此类的实例的方法

(4)创建指定枚举和值的实例的方法。

举例:enum Colors { Red, Green, Blue, Yellow };

Enum–>String

(1)利用Object.ToString()方法:如Colors.Green.ToString()的值是"Green"字符串;

(2)利用Enum的静态方法GetName与GetNames:

public static string GetName(Type enumType,Object value)

public static string[] GetNames(Type enumType)

例如:Enum.GetName(typeof(Colors),3))与Enum.GetName(typeof(Colors), Colors.Blue))的值都是"Blue"

Enum.GetNames(typeof(Colors))将返回枚举字符串数组。

String–>Enum

(1)利用Enum的静态方法Parse:

public static Object Parse(Type enumType,string value)

例如:(Colors)Enum.Parse(typeof(Colors), "Red")

Enum–>Int

(1)因为枚举的基类型是除 Char 外的整型,所以可以进行强制转换。

例如:(int)Colors.Red, (byte)Colors.Green

Int–>Enum

(1)可以强制转换将整型转换成枚举类型。

例如:Colors color = (Colors)2 ,那么color即为Colors.Blue

(2)利用Enum的静态方法ToObject。

public static Object ToObject(Type enumType,int value)

例如:Colors color = (Colors)Enum.ToObject(typeof(Colors), 2),那么color即为Colors.Blue

判断某个整型是否定义在枚举中的方法:Enum.IsDefined

public static bool IsDefined(Type enumType,Object value)

例如:Enum.IsDefined(typeof(Colors), n))

java的枚举转c#

有段java的enum定义

public enum ConstantKind {
 
    CONSTANT_Int (0x03),
    CONSTANT_UInt (0x04),
    CONSTANT_Double (0x06),
    CONSTANT_Utf8 (0x01),
    CONSTANT_True (0x0B),
    CONSTANT_False (0x0A),
    CONSTANT_Null (0x0C),
    CONSTANT_Undefined (0x00),
    CONSTANT_Namespace (0x08),
    CONSTANT_PackageNamespace (0x16),
    CONSTANT_PackageInternalNs (0x17),
    CONSTANT_ProtectedNamespace (0x18),
    CONSTANT_ExplicitNamespace (0x19),
    CONSTANT_StaticProtectedNs (0x1A),
    CONSTANT_PrivateNs (0x05);
     
    private static Map<Integer, ConstantKind> constantKindMap = new HashMap<Integer, ConstantKind>();
     
    static {
        for (ConstantKind constantKind : ConstantKind.values()) {
            constantKindMap.put(constantKind.constantKind, constantKind);
        }
    }
 
    private final int constantKind;
 
    ConstantKind(int constantKind){
        this.constantKind =  constantKind;
    }
 
    public int getConstantKind() {
        return constantKind;
    }
     
    public static ConstantKind getConstant(int constantKind){
        return constantKindMap.get(constantKind);
    }
     
    public Object getValueReal(int vIndex, CpoolInfo cpool){
         
        switch(this){
            case CONSTANT_Int: return cpool.getInts().get(vIndex);
            case CONSTANT_UInt: return cpool.getUints().get(vIndex);
            case CONSTANT_Double: return cpool.getDoubles().get(vIndex);
            case CONSTANT_Utf8: return cpool.getStrings().get(vIndex);
            case CONSTANT_True: return true;
            case CONSTANT_False: return false;
            case CONSTANT_Null: return null;
            case CONSTANT_Undefined: return null;
            case CONSTANT_Namespace: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_PackageNamespace: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_PackageInternalNs: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_ProtectedNamespace: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_ExplicitNamespace: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_StaticProtectedNs: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_PrivateNs: return cpool.getNamespaces().get(vIndex);
        }
         
        return null;
    }
     
}

并有一个调用的方法写法如下:

public String getVkind_string() {
    ConstantKind cKind = ConstantKind.getConstant(vkind);
    if(cKind != null){
        return ConstantKind.getConstant(vkind).name();
    }else{
        return "";
    }
}

如果翻译到C#, 可以这么写:

    public enum ConstantKind
    {
        CONSTANT_Int = 0x03,
        CONSTANT_UInt = 0x04,
        CONSTANT_Double = 0x06,
        CONSTANT_Utf8 = 0x01,
        CONSTANT_True = 0x0B,
        CONSTANT_False = 0x0A,
        CONSTANT_Null = 0x0C,
        CONSTANT_Undefined = 0x00,
        CONSTANT_Namespace = 0x08,
        CONSTANT_PackageNamespace = 0x16,
        CONSTANT_PackageInternalNs = 0x17,
        CONSTANT_ProtectedNamespace = 0x18,
        CONSTANT_ExplicitNamespace = 0x19,
        CONSTANT_StaticProtectedNs = 0x1A,
        CONSTANT_PrivateNs = 0x05
}

调用时这么写

public String getVkind_string() {
    ConstantKind cKind; 
    Enum.TryParse<ConstantKind>(vkind.ToString("x"), true, out cKind);
    if(cKind != null){
        return Enum.GetName(typeof(ConstantKind), cKind);     
      }else{
        return "";
    }
}

Linux添加硬盘方法

Linux的硬盘识别:
sda        表示第1块SCSI硬盘
hda       表示第1块IDE硬盘

scd0      表示第1个USB光驱

一般使用“fdisk -l”命令可以列出系统中当前连接的硬盘
步骤:  
1.关闭服务器加上新硬盘
2.启动服务器,以root用户登录
3.查看硬盘信息
Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
  
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14       60801   488279610   8e  Linux LVM
  
Disk /dev/sdb: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
  
Disk /dev/sdb doesn't contain a valid partition table

Disk /dev/sdb doesn't contain a valid partition table, 代表sdb还没有分区和格式化。

4.创建新硬盘分区

fdisk可以用m命令来看fdisk命令的内部命令;
a:命令指定启动分区;
d:命令删除一个存在的分区;
l:命令显示分区ID号的列表;
m:查看fdisk命令帮助;
n:命令创建一个新分区;
p:命令显示分区列表;
t:命令修改分区的类型ID号;
w:命令是将对分区表的修改存盘让它发生作用。
fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
  
  
The number of cylinders for this disk is set to 60801.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
  
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-60801, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-60801, default 60801): 
Using default value 60801
  
Command (m for help): w
The partition table has been altered!
  
Calling ioctl() to re-read partition table.
Syncing disks.
输入n回车,再输入p回车,再输出1回车,一路回车,最后输入w回车保存。

查看一下:fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
  
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14       60801   488279610   8e  Linux LVM
  
Disk /dev/sdb: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
  
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       60801   488384001   83  Linux

可以看到/dev/sdb1分区

5.格式化分区:

#mkfs -t ext3 -c /dev/sdb1

mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
61063168 inodes, 122096000 blocks
6104800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
3727 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
    102400000
  
Checking for bad blocks (read-only test): done                              
Writing inode tables: done                          
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
  
This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

6.创建/data目录:

mkdir /data
7.挂载分区:
mount /dev/sdb1 /data
8.开机后自动挂载
#vim /etc/fstab
9.在最后一行添加如下内容:
/dev/sdb1               /data                    ext3    defaults        0  0
10.测试
umount /data/
mount -a /data
df -h

在win2003上使用iis6+fastcgi+php安装sugarce6.2

1.下载安装fastcgi-for-iis, http://www.iis.net/downloads/microsoft/fastcgi-for-iis

2.下载解压php5.3 for windows,

3.注册PHP到FastCGI
在命令行下运行
cscript fcgiconfig.js -add -section:"PHP" -extension:php -path:"D:\php5.3\php-cgi.exe"

或者直接修改C:\WINDOWS\system32\inetsrv\fcgiext.ini文件,加入

[Types]
php=PHP
  
[PHP]
ExePath="D:\php5.3\php-cgi.exe"

如果有必要,可以更改fcgiext.dll和fcgiext.ini的权限为iuser和iis_wp可读写。


4.修改php.ini

extension_dir = "D:\php5.3\ext"
fastcgi.impersonate = 1 
cgi.fix_pathinfo=1 
cgi.force_redirect = 0 
session.auto_start = 0
session.use_cookies = 1

为了支持sugarcrm,则还需要另外开启mbstring,imap,zip等扩展

5.IIS中新建站点,在“主目录”-》“配置”-》“应用程序配置”中设定.php文件映射为windows/system/inetsrv/fcgiext.dll

动作可以限制为:GET,HEAD,POST

6.下载有语言包的版本http://www.sugarforge.org/frs/?group_id=6,我下载的是sugarce6.2,新建站点,给站点目录设为IIS_WPG可读写。

7.安装,一路next,下载对应语言包http://sugar360.cn/portal.php?mod=view&aid=3,登录后"系统管理"-》module loader,上传语言包,注销,重新登录后语言包升效。

C#文件上传时判断图片文件格式[转]

转自:http://www.cnblogs.com/gzlxm/archive/2010/05/25/1743296.html

平时在上传文件文件时,只允许上传jpg,gif,png等后缀的图片文件,这个可以根据文件后缀名来过滤。但是如果用户修改了后缀名来欺骗系统的话又该怎么解决?比如a.txt格式的改成a.jpg,我现在的程序就无法识别了,虽然在后台可以弹出错误,但这个错误已经不是FS上定义的错误了。

解决:

在网上查了好多资料,大部分都是通过将文件读成二进制流,取前两个字节判断,比如.jpg的是255216.代码如下:

public static FileExtension CheckTextFile(string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
            string fileType = string.Empty; ;
            try
            {
                byte data = br.ReadByte();
                fileType += data.ToString();
                data = br.ReadByte();
                fileType += data.ToString();
                FileExtension extension;
                try
                {
                    extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
                }
                catch
                { 
 
                    extension=FileExtension.VALIDFILE
                }
                return extension;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    br.Close();
                }
            }
        }
    }
    public enum FileExtension
    {
        JPG = 255216,
        GIF = 7173,
        PNG = 13780,
        SWF = 6787,
        RAR = 8297,
        ZIP = 8075,
        _7Z = 55122,
        VALIDFILE=9999999
        // 255216 jpg;  
 
        // 7173 gif;  
 
        // 6677 bmp,  
 
        // 13780 png;  
 
        // 6787 swf  
 
        // 7790 exe dll,  
 
        // 8297 rar  
 
        // 8075 zip  
 
        // 55122 7z  
 
        // 6063 xml  
 
        // 6033 html  
 
        // 239187 aspx  
 
        // 117115 cs  
 
        // 119105 js  
 
        // 102100 txt  
 
        // 255254 sql   
 
    }

经过测试,可以很好的判断.jpg、.gif格式的文件.

然而对于.txt文件却不是102100,每个.txt返回的值也不一样。显然,这个方法不能满足我的需要。

     后来看到一个delphi写的,这里有一个很简单的方法:把给定的那个文件看作是无类型的二进制文件,然后顺序地读出这个文件的每一个字节,如果文件里有一个字节的值等于0,那么这个文件就不是文本文件;反之,如果这个文件中没有一个字节的值是0的话,就可以判定这个文件是文本文件了。这是原理,下面看看在Delphi 中怎样编程来实现它--
   

复制代码
function IsTextFile(FileName:string):boolean; 
var
Fs:TFileStream;
i,size:integer;
IsTextFile:boolean;
ByteData:Byte;
begin
if FileExists(FileName) then
begin
Fs:
=TFileStream.Create(FileName,fmOpenRead);
IsTextFile:
=true;
i:
=0;
size:
=Fs.Size;
While (i
<size) and IsTextFile do
begin
Fs.Read(ByteData,
1);
IsTextFile:
=ByteData<>0;
inc(i)
end;
Fs.Free;
Result:
=IsTextFile
end
else
Result:
=false
end;
复制代码

    我把它翻译成C#代码后是这样的:

复制代码
///<summary>
/// Checks the file is textfile or not.
///</summary>
///<param name="fileName">Name of the file.</param>
///<returns></returns>
publicstaticbool CheckIsTextFile(string fileName)
       {
           FileStream fs
=new FileStream(fileName, FileMode.Open, FileAccess.Read);
bool isTextFile=true;
try
           {
int i =0;
int length = (int)fs.Length;
byte data;
while (i < length && isTextFile)
               {
                   data
= (byte)fs.ReadByte();
                   isTextFile
= (data !=0);
                   i
++;
               }
return isTextFile;
           }
catch (Exception ex)
           {
throw ex;
           }
finally
           {
if (fs !=null)
               {
                   fs.Close();
               }
           }
       }
复制代码

 后来经过测试,满足了我的需求。

 附测试代码:

复制代码
bool isTextFile = Utility.CheckIsTextFile(this.openFile.FileName);
if (isTextFile)
{
                   this.richTxtContent.AppendText(openFile.FileName + "是文本文件");
}
else
{
                   this.richTxtContent.AppendText(openFile.FileName + "不是文本文件!");
}
复制代码

unexpected inconsistency; run fsck manually

今天一台服务器突然登录不上,无乃求助机房,重启后提示如图:

中间曾被建议重装系统,因为有很多环境要配置,而且盘上有很多重要历史数据,又重试了一把,幸亏成功。

后来查了一下fsck命令,使用说明如下:

fsck(file system check)用来检查和维护不一致的文件系统。若系统掉电或磁盘发生问题,可利用fsck命令对文件系统进行检查
Linux技巧:使用Fsck命令修复损坏的分区
输入Root密码登录后采用:fsck –t ext3 -r /usr/local 修复
fsck的命令的几个使用方法
指令:fsck
使用权限 : 超级使用者 root
使用方式 : fsck [-sACVRP] [-t fstype] [–] [fsck-options] filesys […]
说明 : 检查与修复 Linux 档案系统,可以同时检查一个或多个 Linux 档案系统
参数 :
filesys : device 名称(eg./dev/sda1),mount 点 (eg. / 或 /usr)
-t : 给定档案系统的型式,若在 /etc/fstab 中已有定义或 kernel 本身已支援的则不需加上此参数
-s : 依序一个一个地执行 fsck 的指令来检查
-A : 对/etc/fstab 中所有列出来的 partition 做检查
-C : 显示完整的检查进度
-d : 列印 e2fsck 的 debug 结果
-p : 同时有 -A 条件时,同时有多个 fsck 的检查一起执行
-R : 同时有 -A 条件时,省略 / 不检查
-V : 详细显示模式
-a : 如果检查有错则自动修复
-r : 如果检查有错则由使用者回答是否修复
-y : 选项指定检测每个文件是自动输入yes,在不确定那些是不正常的时候,可以执行 # fsck -y 全部检查修复。[1]
例子 :
检查 msdos 档案系统的 /dev/hda5 是否正常,如果有异常便自动修复 :fsck -t msdos -a /dev/hda5
注意 :
此指令可与 /etc/fstab 相互参考操作来加以了解。
简介与应用
The system utility fsck (for "file system check" or "file system consistency check") is a tool for checking the consistency of a file system in the Unix system and clones thereof.
Generally, fsck is run automatically at boot time when the system detects that a file system is in an inconsistent state, indicating a non-graceful shutdown, such as a crash or power loss. Typically, fsck utilities provide options for either interactively repairing damaged file systems (the user must decide how to fix specific problems), automatically deciding how to fix specific problems (so the user doesn't have to answer any questions), or reviewing the problems that need to be resolved on a file system without actually fixing them.
Fsck can also be run manually by the system administrator if there is believed to be a problem with the file system. However, running fsck on a mounted file system can potentially cause severe data corruption/loss.
A journaling file system is designed such that tools such as fsck do not need to be run as often. The UFS2 Filesystem in FreeBSD has background fsck, so it is usually not necessary to wait for fsck to finish before accessing the disk.
The Microsoft equivalent programs are scandisk and chkdsk. In DOS, chkdsk was used to check file system integrity, but later versions also featured a tool called scandisk for checking disk surface for bad sectors. Windows 95, Windows 98 and Windows Me featured a version of scandisk which ran natively in the Windows environment. The Windows NT series, including NT 4, Windows 2000, and Windows XP all use a modern, updated version of chkdsk which performs both disk surface and file system scanning, though its disk-surface check is less thorough than scandisk's.
Use as profanity
Before the rise of journaling file systems, it was not uncommon for an improperly shut-down Unix system's file system to develop a corrupted superblock. This possibly serious problem could only be resolved by running fsck, which could take anywhere from a few seconds to hours, depending on the volume's size. Because of the severity of fsck not being able to resolve this error, the terms "fsck" and "fscked" have come into use among Unix system administrators as a minced oath. When one is told "Go fsck yourself!" the meaning implied is to "go away, analyze yourself, and fix your problems", mirroring fsck's primary function.
It is unclear whether this usage was cause or effect, as it has been anecdotally reported that Dennis Ritchie has claimed "The second letter was originally different." [1]
Pronunciation
Like many computer jargon terms without vowels, fsck is essentially unpronounceable in most languages and so people are forced to improvise when speaking it aloud. It can be pronounced "F-S-C-K," "F-S-check," "fisk," "fizik," "fuss-uck," "fizz-uck," "fozz-uck," "F-sick," "F-suck" (associated with how long it takes to run [2]), "F-sek," or "fusk." Some prefer to just pronounce it as "fuck".
References
^ Google USENET archive of a 1998 post to alt.sysadmin.recovery
^ Example Google Groups post
External links
man fsck
Checking and Repairing File system with fsck
Jargon File entry: fscking
The many faces of fsck