国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:22:10
字體:
供稿:網(wǎng)友
一..Net Framework

1.  如何獲得系統(tǒng)文件夾

使用System.Envioment類的GetFolderPath方法;例如:

Environment.GetFolderPath( Environment.SpecialFolder.Personal )

2.  如何獲得正在執(zhí)行的exe文件的路徑

1)  使用Application類的ExecutablePath屬性

2)  System.Reflection.Assembly.GetExecutingAssembly().Location

3.  如何檢測操作系統(tǒng)的版本

使用Envioment的OSVersion屬性,例如:

OperatingSystem os = Environment.OSVersion;

MessageBox.Show(os.Version.ToString());

MessageBox.Show(os.Platform.ToString());

4.  如何根據(jù)完整的文件名獲得文件的文件名部分、

使用System.IO.Path類的方法GetFileName或者GetFileNameWithoutExtension方法

5.  如何通過文件的全名獲得文件的擴展名

使用System.IO.Path.GetExtension靜態(tài)方法

6.  Vb和c#的語法有什么不同click here

7.  如何獲得當前電腦用戶名,是否聯(lián)網(wǎng),幾個顯示器,所在域,鼠標有幾個鍵等信息

使用System.Windows.Forms. SystemInformation類的靜態(tài)屬性

8.  修飾Main方法的[STAThread]特性有什么作用

標示當前程序使用單線程的方式運行

9.  如何讀取csv文件的內(nèi)容 

通過OdbcConnection可以創(chuàng)建一個鏈接到csv文件的鏈接,鏈接字符串的格式是:"Driver={Microsoft Text Driver (*.txt;*.csv)};Dbq="+cvs文件的文件夾路徑+"          Extensions=asc,csv,tab,txt; Persist Security Info=False";

創(chuàng)建連接之后就可以使用DataAdapter等存取csv文件了。

詳細信息見此處

10. 如何獲得磁盤開銷信息,代碼片斷如下,主要是調(diào)用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.如何獲得不區(qū)分大小寫的子字符串的索引位置

         1)通過將兩個字符串轉(zhuǎn)換成小寫之后使用字符串的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)  

一種更優(yōu)雅的方法是使用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); 

 



. OOPs 
1. 什么是復制構(gòu)造函數(shù)

  我們知道構(gòu)造函數(shù)是用來初始化我們要創(chuàng)建實例的特殊的方法。通常我們要將一個實例賦值給另外一個變量c#只是將引用賦值給了新的變量實質(zhì)上是對同一個變量的引用,那么我們怎樣才可以賦值的同時創(chuàng)建一個全新的變量而不只是對實例引用的賦值呢?我們可以使用復制構(gòu)造函數(shù)。

我們可以為類創(chuàng)造一個只用一個類型為該類型的參數(shù)的構(gòu)造函數(shù),如:




public Student(Student student)
{
 this.name = student.name;
}

 

使用上面的構(gòu)造函數(shù)我們就可以復制一份新的實例值,而非賦值同一引用的實例了。

class Student
{
     private string name;

     public Student(string name)
     {
         this.name = name;
     }
     public Student(Student student)
     {
         this.name = student.name;
     }

    public string Name 
    {
       get 
       {
              return name; 
       }
       set 
       {
            name = value; 
       }
    }
}

class Final

{

    static void Main()

      {

        Student student = new Student ("A");

        Student NewStudent = new Student (student);

        student.Name = "B";

        System.Console.WriteLine("The new student's name is {0}", NewStudent.Name);

      }

}

 

The new student's name is A.

2.什么是只讀常量

就是靜態(tài)的只讀變量,它通常在靜態(tài)構(gòu)造函數(shù)中賦值。 

class Numbers
{
    public readonly int m;
    public static readonly int n;

    public Numbers (int x)
    {
       m=x;
    }

    static Numbers ()
    {
        n=100;
    }

 } //其中n就是一個只讀的常量,對于該類的所有實例他只有一種值,而m則根據(jù)實例不同而不同

 

三.VS.Net IDE

1. 2請看原作

3.如何改變region的顏色

   通過工具 à 選項 à 環(huán)境 à 字體和顏色 à 可折疊文本設置

 

四.WinForm

1.如何使winForm不顯示標題欄?

通過設置form的Text屬性為空字符串,設置ControlBox屬性為false

form1.Text = string. Empty; 

form1.ControlBox = false;

2.如何使winform的窗體使用XP的風格

見原作

3.如何禁止form在工具欄顯示

設置form的ShowInTaskbar屬性為false即可

4.如何使程序打開默認的郵件程序并帶有一些參數(shù)讓用戶開始寫郵件

         1)如果是web程序:

         <a href=”mailto:email@address1.com,email@address2.com?cc=email@address3.com&Subject=Hello&body=Happy New Year”>some text</a>

         2) 對于windows程序,需要使用System.Diagnostics.Process類

Process process = new Process();
process.StartInfo.FileName = "mailto:email@address1.com,email@address2.com?subject=Hello&cc=email@address3.com
&bcc=email@address4.com&body=Happy New Year" ;

process.Start();


5.如何創(chuàng)建類似msn提示窗口

需要獲得通過Screen.GetWorkingArea(this).Width(Height)屬性獲得屏幕的大小,然后使用一個timer根據(jù)時間改變窗口的位置

五.Button控件

1.如何設置form的默認button(即在form上按下回車時觸發(fā)的button)

         可以設置form的AcceptButton屬性:form1.AcceptButton = button1;

2. 如何設置form的取消button(即在用戶按下Esc鍵時觸發(fā)的button)

         可以設置form的CancelButton屬性:form1.CancelButton = buttonC;

3. 如何通過程序觸發(fā)一個button的Click事件

         Button1.PerformClick

 

六.Combo Box

1.如何使用可選字體填充Combo Box

comboBox1.Items.AddRange (FontFamily.Families);

 

七.TextBox

1.如何禁用TextBox的默認上下文菜單(右鍵菜單)

textBox1.ContextMenu = new ContextMenu();

2,3 見原作

4.如何在TextBox獲得焦點的時候,將焦點放在textBox文字的最后

textBox1.SelectionStart = textBox1.Text.Length;

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 龙山县| 五华县| 建阳市| 晋州市| 博客| 罗定市| 蓝田县| 扶沟县| 白银市| 慈利县| 威远县| 长白| 阿克苏市| 洪泽县| 南郑县| 雅江县| 黄陵县| 治县。| 闽侯县| 方山县| 长武县| 襄樊市| 永宁县| 博爱县| 乡宁县| 项城市| 琼结县| 大连市| 江陵县| 平邑县| 宁强县| 犍为县| 鹿邑县| 兰溪市| 洮南市| 甘肃省| 营口市| 武夷山市| 顺义区| 桐庐县| 夹江县|