stand on the shoulders of giants

.NET / C# basic

非托管与托管类型对比

IntPtr表示指针或句柄的平台特定类型,使用WINDOW   API时使用。就是void* 就是HANDLE, 具体如下表:

Wtypes.h 中的非托管类型         非托管C   语言类型         托管类名               说明    
  HANDLE                                   void*                           System.IntPtr       32  
  
  BYTE                                unsigned   char               System.Byte         8  
   
  SHORT                               short                           System.Int16       16  
   
  WORD                               unsigned   short              System.UInt16     16  
   
  INT                                  int                              System.Int32       32  
   
  UINT                                 unsigned   int                 System.UInt32     32  
   
  LONG                                long                            System.Int32       32  
  
  BOOL                                long                           System.Int32       32  
      
  DORD                              unsigned   long                System.UInt32     32      
  ULONG                             unsigned   long               System.UInt32     32  
   
  CHAR                                 char                   System.Char           
ANSI 修饰
  LPSTR                               char*                   System.String
System.StringBuilderANSI修饰
  LPCSTR                             Const   char*          System.String
System.StringBuilderANSI修饰
  LPWSTR                           wchar_t*               System.String
System.StringBuilderUnicode修饰
  LPCWSTR                         Const   wchar_t*        System.String
System.StringBuilderUnicode修饰   
  FLOAT                             Float                      System.Single          32  
   
  DOUBLE                           Double                   System.Double          64  
    

获取程序目录的方法:

1.   System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
    
获取模块的完整路径。
2.   System.Environment.CurrentDirectory
    
获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
3.   System.IO.Directory.GetCurrentDirectory() 
4.   System.AppDomain.CurrentDomain.BaseDirectory
    
获取程序的基目录。
5.  System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
    
获取和设置包括该应用程序的目录的名称。
6.  System.Windows.Forms.Application.StartupPath
    
获取启动了应用程序的可执行文件的路径。效果和25一样。只是5返回的字符串后面多了一个"\"而已
7.  System.Windows.Forms.Application.ExecutablePath
    
获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。

Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)

读文件代码片段

代码
public class TestReadFile 

    
public static void Main(String[] args) 
    { 
        
// Read text file C:\temp\test.txt 
        FileStream fs = new FileStream(@"c:\temp\test.txt" , FileMode.Open, FileAccess.Read); 
        StreamReader sr 
= new StreamReader(fs);  
        
        String line
=sr.ReadLine();
        
while (line!=null)
        {
            Console.WriteLine(line);
            line
=sr.ReadLine();
        }   
        
        sr.Close();
        fs.Close();
    } 

 

写文件代码片段

代码
using System; 
using System.IO; 
public class TestWriteFile 

    
public static void Main(String[] args) 
    { 
        
// Create a text file C:\temp\test.txt 
        FileStream fs = new FileStream(@"c:\temp\test.txt" , FileMode.OpenOrCreate, FileAccess.Write); 
        StreamWriter sw 
= new StreamWriter(fs); 
        
// Write to the file using StreamWriter class 
        sw.BaseStream.Seek(0, SeekOrigin.End); 
        sw.WriteLine(
" First Line "); 
        sw.WriteLine(
" Second Line"); 
        sw.Flush(); 
    } 

 

取得IP

代码
{
     
public static void Main()
     {
         IPHostEntry ipEntry 
= Dns.GetHostByName ("localhost");
         IPAddress [] IpAddr 
= ipEntry.AddressList;
         
for (int i = 0; i < IpAddr.Length; i++)
         { 
             Console.WriteLine (
"IP Address {0}: {1} ", i, IpAddr[i].ToString ());
         }
    }
}

 

根据IP取得机器名

代码
class ResolveIP
{
     
public static void Main()
     {
         IPHostEntry ipEntry 
= Dns.Resolve("172.29.9.9");
         Console.WriteLine (
"Host name : {0}", ipEntry.HostName);         
     }
}

 

关于Assembly的动态加载

Assembly的加载通常是由CLR隐式进行的,CLR的原则是只有用到时才加载,也就是说很多时候在应用程序运行过程中还要进行加载Assembly的动作,如果希望显式的在可预期的时候动态加载Assembly, 可以采用以下方法:

Assembly
的动态加载有两种主要形式,都是使用Assembly类的静态方法:

* Assembly.LoadFrom(FilePath|URI):
直接指定Assembly的文件路径或URI,调用Assembly Loader

* Assembly.Load(FullQualifiedAssemblyName)
:需要输入Assembly的全名称(包括name, version, culture, public key token),该种加载调用Assembly resolver,并基于以下策略来定位相应的Assembly:
   1. Assembly
是否有public key,如果没有直接到Probing (step 5),如果有则应用version policy
   2.
基于version policy,也就是.config文件种的assemblyBinding元素下assembly定位的信息;
   3.
如果无法通过version policy定位,则在GAC种查找;
   4.
如果无法在GAC中定位assemblycodeBase来定位,如果通过codeBase找到的文件不匹配,则失败;
   5.
如果无法通过codeBase定位,则通过probing方式来查找,如果找到的文件不匹配,则失败。
   6.
在第一步之前应该还可以在DEVPATH系统变量中查找,但一般这只是给开发者用的而不是在部署时用的,所以可以忽略不计。

以上两种方式表面上类似,但一个是调用Loader,一个调用Resolver,实际差别很大。

 

posted @ 2010-05-14 01:01  DylanWind  阅读(240)  评论(0编辑  收藏  举报