extern外部方法使用C#的實現(xiàn)方法
本文實例講述了extern外部方法使用C#的方法。分享給大家供大家參考。具體分析如下:
外部方法使用C#步驟如下:
1、增加引用using System.Runtime.InteropServices;
2、聲明和實現(xiàn)的連接[DllImport("kernel32", SetLastError = true)]
3、聲明外部方法public static extern int GetCurrentDirectory(int a, StringBuilder b);
4、對外部方法操作 GetCurrentDirectory(300, pathstring);
具體實現(xiàn)代碼如下:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;//引用外部
namespace extern
{
public partial class DllImportForm : Form
{
public DllImportForm()
{
InitializeComponent();
}
[DllImport("kernel32", SetLastError = true)]//聲明和實現(xiàn)的連接
public static extern int GetCurrentDirectory(int a, StringBuilder b);//外部方法
private void btnDisplay_Click(object sender, EventArgs e)
{
StringBuilder pathstring=new StringBuilder ();//返回路徑
GetCurrentDirectory(300, pathstring);
this.listBox1.Items.Add (pathstring );
}
}
}
希望本文所述對大家的C#程序設計有所幫助。
相關文章
C#實現(xiàn)xml文件反序列化讀入數(shù)據(jù)到object的方法
這篇文章主要介紹了C#實現(xiàn)xml文件反序列化讀入數(shù)據(jù)到object的方法,涉及C#操作XML文件類型轉換的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
C# DataTable中Compute方法用法集錦(數(shù)值/字符串/運算符/表等操作)
這篇文章主要介紹了C# DataTable中Compute方法用法,總結分析了DataTable中Compute方法常見的數(shù)值運算操作、字符串操作、運算符操作、表運算等相關技巧,需要的朋友可以參考下2016-06-06
C#調(diào)用SQL?Server中有參數(shù)的存儲過程
這篇文章介紹了C#調(diào)用SQL?Server中有參數(shù)存儲過程的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題
這篇文章主要介紹了C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題 ,文中給大家列舉通過兩種方法來判斷,需要的朋友可以參考下2018-10-10
C#實現(xiàn)延時并自動關閉MessageBox的方法
這篇文章主要介紹了C#實現(xiàn)延時并自動關閉MessageBox的方法,非常實用的功能,需要的朋友可以參考下2014-08-08

