当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net一些很酷很实用的.Net技巧第1/2页

asp.net一些很酷很实用的.Net技巧第1/2页

2018年04月21日  | 移动技术网IT编程  | 我要评论

卡布西游小白狐,混沌穴修诀,广安seo

一..net framework

1.  如何获得系统文件夹

使用system.envioment类的getfolderpath方法;例如:

environment.getfolderpath( environment.specialfolder.personal )

2.  如何获得正在执行的exe文件的路径

1)  使用application类的executablepath属性

2)  system.reflection.assembly.getexecutingassembly().location

3.  如何检测操作系统的版本

使用envioment的osversion属性,例如:

operatingsystem os = environment.osversion;

messagebox.show(os.version.tostring());

messagebox.show(os.platform.tostring());

4.  如何根据完整的文件名获得文件的文件名部分、

使用system.io.path类的方法getfilename或者getfilenamewithoutextension方法

5.  如何通过文件的全名获得文件的扩展名

使用system.io.path.getextension静态方法

6.  vb和c#的语法有什么不同click here

7.  如何获得当前电脑用户名,是否联网,几个显示器,所在域,鼠标有几个键等信息

使用system.windows.forms. systeminformation类的静态属性

8.  修饰main方法的[stathread]特性有什么作用

标示当前程序使用单线程的方式运行

9.  如何读取csv文件的内容 

通过odbcconnection可以创建一个链接到csv文件的链接,链接字符串的格式是:"driver={microsoft text driver (*.txt;*.csv)};dbq="+cvs文件的文件夹路径+"          extensions=asc,csv,tab,txt; persist security info=false";

创建连接之后就可以使用dataadapter等存取csv文件了。

详细信息见此处

10. 如何获得磁盘开销信息,代码片断如下,主要是调用kernel32.dll中的getdiskfreespaceex外部方法。




public sealed class driveinfo
{
    [dllimport("kernel32.dll", entrypoint = "getdiskfreespaceexa")]
    private static extern long getdiskfreespaceex(string lpdirectoryname,
        out long lpfreebytesavailabletocaller,
        out long lptotalnumberofbytes,
        out long lptotalnumberoffreebytes);

    public static long getinfo(string drive, out long available, out long total, out long free)
    {
        return getdiskfreespaceex(drive, out available, out total, out free);
    }

    public static driveinfosystem getinfo(string drive)
    {
        long result, available, total, free;
        result = getdiskfreespaceex(drive, out available, out total, out free);
        return new driveinfosystem(drive, result, available, total, free);
    }
}

public struct driveinfosystem
{
    public readonly string drive;
    public readonly long result;
    public readonly long available;
    public readonly long total;
    public readonly long free;

    public driveinfosystem(string drive, long result, long available, long total, long free)
    {
        this.drive = drive;
        this.result = result;
        this.available = available;
        this.total = total;
        this.free = free;
    }
}




可以通过

driveinfosystem info = driveinfo.getinfo("c:");来获得指定磁盘的开销情况 


11.如何获得不区分大小写的子字符串的索引位置

         1)通过将两个字符串转换成小写之后使用字符串的indexof方法:




string strparent = "the codeproject site is very informative.";

string strchild = "codeproject";

// the line below will return -1 when expected is 4.
int i = strparent.indexof(strchild);

// the line below will return proper index
int j = strparent.tolower().indexof(strchild.tolower());

 

        2)  

一种更优雅的方法是使用system.globalization命名空间下面的compareinfo类的indexof方法: 

 

using system.globalization;

string strparent = "the codeproject site is very informative.";

string strchild = "codeproject";
// we create a object of compareinfo class for a neutral culture or a culture insensitive object
compareinfo compare = cultureinfo.invariantculture.compareinfo;

int i = compare.indexof(strparent,strchild,compareoptions.ignorecase); 

 
1

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网