extern 的用法 及Ini文件的读写 ,@字符的用法

关于Extern修饰符的用法网上其实很多了,这里我来老生常谈一下,以便加深印象。

extern 主要用于声明在外部实现的方法,什么叫外部实用的方法呢,一般说来就是用System.Runtime.InteropServices服务的DllImport方法引入非托管代码程序集。例如调用系统API,C语言写的方法等等。在这种情况下,声明必须为static

同时,extern 关键字还可以定义外部程序集别名,使得可以从单个程序集中引用同一组件的不同版本。

下面是一个改写自MSDN上的简单的例子,调用系统winmm.DLL播放wav文件:

//系统API的调用的声明
        [System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        public static extern void PlaySound(string path,System.IntPtr hMod,PlaySoundFlags flags);

//调用该方法

    string path = @"c:\111.wav";
            try
            {
                PlaySound(path, new System.IntPtr(), PlaySoundFlags.SND_SYNC);
            }
            catch (Exception ex)
            {
                throw (ex);
            }

C#读写ini文件的类,调用kernel32.dll中的api:WritePrivateProfileString,GetPrivateProfileString

using  System.Runtime.InteropServices;

using  System.Text; 

namespace  INIFile

{

     ///  <summary>

     ///  读写ini文件的类

     ///  调用kernel32.dll中的两个apiWritePrivateProfileStringGetPrivateProfileString来实现对ini  文件的读写。

     ///

     ///  INI文件是文本文件,

     ///  由若干节(section)组成,

     ///  在每个带括号的标题下面,

     ///  是若干个关键词(key)及其对应的值(value)

     /// 

  ///[Section]

  ///Key=value

     ///

     ///  </summary>

     public  class  IniFile

 

     {

         ///  <summary>

         ///  ini文件名称(带路径)

         ///  </summary>

         public  string  filePath; 

 

         //声明读写INI文件的API函数

         [DllImport("kernel32")] 

         private  static  extern  long  WritePrivateProfileString(string  section,string  key,string  val,string  filePath);

 

         [DllImport("kernel32")]

         private  static  extern  int  GetPrivateProfileString(string  section,string  key,string  def,StringBuilder  retVal,int  size,string  filePath);

 

         ///  <summary>

         ///  类的构造函数

         ///  </summary>

         ///  <param  name="INIPath">INI文件名</param> 

         public  IniFile(string  INIPath)   

         { 

              filePath  =  INIPath;

 

         }

 

         ///  <summary>

         ///   INI文件

         ///  </summary>

         ///  <param  name="Section">Section</param>

         ///  <param  name="Key">Key</param>

         ///  <param  name="value">value</param>

         public  void  WriteInivalue(string  Section,string  Key,string  value)   

         {     

              WritePrivateProfileString(Section,Key,value,this.filePath);

 

         }

 

         ///  <summary>

         ///    读取INI文件指定部分

         ///  </summary>

         ///  <param  name="Section">Section</param>

         ///  <param  name="Key">Key</param>

         ///  <returns>String</returns> 

         public  string  ReadInivalue(string  Section,string  Key) 

         {   

              StringBuilder  temp  =  new  StringBuilder(255);

              int  i  =  GetPrivateProfileString(Section,Key,"",temp,255,this.filePath); 

              return  temp.ToString();

 

         } 

     } 

}

@符号是特殊而又实用的C#符号。

比如它在string中的应用。

1

字符@表示,其后的字符串是个“逐字字符串”(verbatim string)。 // 这个说法来自C# Primer 中文版(Stanley B. Lippman, 侯捷/陈硕合译)

2

对于逐字字符串字面变量(verbatim string literal ),我们不再需要使用“转义序列”就可以指定反斜线之类的特殊字符。@的这个特点使得在表示文件路径时很方便。

如:

string str = @"C:\Test.txt";

3

另外一点,用@表示的字符串能够跨越数行。这数行之内的空白字符(White Space)都会保留在字符串里。

这样便能允许存储和生成带有格式的文本块。

如:

string strText = @"Line1

Line2

Line3";

有意思的是如果在VS.NET2003中当你输入完第一行(string strText = @"Line1)换行后,光标会自动到第二行最开头 ^_^。很智能化、人性化的判断。

4

不知道大家在最初看到@的功能时有没有想,如果“转义序列”(\)在字符串中“失效”,那么想包含一个双引号("),怎么办?我找到了答案。

方法很简单。在双引号之前再加一个双引号即可。

如:

string str = @"""Great!""Said Allen Lee";

5

这仅仅是@在字符串中的用法,有机会再去看看@的其他东东。

posted on 2011-04-07 10:09  Jack.leung 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jack-liang/archive/2011/04/07/2007526.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值