外部方法使用C#簡單例子
1、增加引用using System.Runtime.InteropServices;
2、聲明和實現的連接[DllImport("kernel32", SetLastError = true)]
3、聲明外部方法public static extern int GetCurrentDirectory(int a, StringBuilder b);
4、對外部方法操作 GetCurrentDirectory(300, pathstring);
using System; 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)]//聲明和實現的連接 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 ); } } }