当前位置:编程学习 > C#/ASP.NET >>

C#的一些基础知识点记录

这个最近做一个程序时用到的一些知识点,特此记录想来以备后用!
 
1、测试代码执行时间的方法
 
[csharp]  
Stopwatch sw = new Stopwatch();  
sw.Start();  
//这里填写要执行的代码  
sw.Stop();  
Console.WriteLine("总运行时间:" + sw.Elapsed);  
Console.WriteLine("测量实例得出的总运行时间(毫秒为单位):" + sw.ElapsedMilliseconds);  
Console.WriteLine("总运行时间(计时器刻度标识):" + sw.ElapsedTicks);  
Console.WriteLine("计时器是否运行:" + sw.IsRunning.ToString());  
 
2、文件路径及文件名字符串截取替换操作方法
[html]  
string filePath = @"E:\Randy0528\中文目录\JustTest.rar";  
Response.Write("文件路径:"+filePath);  
Response.Write("<br/>更改路径字符串的扩展名。<br/>");  
Response.Write(System.IO.Path.ChangeExtension(filePath, "txt"));  
Response.Write("<br/>返回指定路径字符串的目录信息。。<br/>");  
Response.Write(System.IO.Path.GetDirectoryName(filePath));  
Response.Write("<br/>返回指定的路径字符串的扩展名。<br/>");  
Response.Write(System.IO.Path.GetExtension(filePath));  
Response.Write("<br/>返回指定路径字符串的文件名和扩展名。<br/>");  
Response.Write(System.IO.Path.GetFileName(filePath));  
Response.Write("<br/>返回不具有扩展名的指定路径字符串的文件名。<br/>");  
Response.Write(System.IO.Path.GetFileNameWithoutExtension(filePath));  
Response.Write("<br/>获取指定路径的根目录信息。<br/>");  
Response.Write(System.IO.Path.GetPathRoot(filePath));  
Response.Write("<br/>返回随机文件夹名或文件名。<br/>");  
Response.Write(System.IO.Path.GetRandomFileName());  
Response.Write("<br/>创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径。<br/>");  
Response.Write(System.IO.Path.GetTempFileName());  
Response.Write("<br/>返回当前系统的临时文件夹的路径。<br/>");  
Response.Write(System.IO.Path.GetTempPath());  
Response.Write("<br/>确定路径是否包括文件扩展名。<br/>");  
Response.Write(System.IO.Path.HasExtension(filePath));  
Response.Write("<br/>获取一个值,该值指示指定的路径字符串是包含绝对路径信息还是包含相对路径信息。<br/>");  
Response.Write(System.IO.Path.IsPathRooted(filePath));  
 
3、System.IO.StreamWriter写文件换行
[html]  
StreamWriter swPdfChange = new StreamWriter(txtfilename, false, Encoding.UTF8);  
swPdfChange.Write("baPWV:" + str1 + " " + str2 + "\r\nABI:" + str3);  
swPdfChange.Close();  
[csharp]  
StreamWriter swPdfChange = new StreamWriter(txtfilename, false, Encoding.UTF8);  
swPdfChange.WriteLine("baPWV:" + str1 + " " + str2 );  
swPdfChange.WriteLine("ABI:" + str3);  
swPdfChange.Close();  
 
4、控制台带参数程序
 
[csharp] 
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
namespace _1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            if (args.Length < 1)  
            {  
                Console.WriteLine("请输入参数 -a -v \"a s\" ");  
            }  
            else  
            {  
                foreach (string key in args)  
                {  
                    if (key == "a s")  
                    {  
                        Console.WriteLine("This is ‘a s' parameters");  
                    }  
                    else if (key == "-a")  
                    {  
                        Console.WriteLine("This is ‘a' parameters");  
                    }  
                    else if (key == "-v")  
                    {  
                        Console.WriteLine("This is ‘v' parameters");  
                    }  
                    else  
                    {  
                        Console.WriteLine("参数错误");  
                    }  
                }  
            }  
        }  
    }  
}  
 
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,