const int GENERIC_READ = 0x80000000;
const int GENERIC_WRITE = 0x40000000;
const int OPEN_EXISTING = 3;[DllImport("kernel32.dll")
public static extern IntPtr CreateFileA(string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
private void Button1_Click(Object sender, EventArgs e)
{
IntPtr iHandle = CreateFileA("LPT1", GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if(iHandle.ToInt32() == -1)
{
MessageBox.Show("can not open port");
}
else
{
MessageBox.Show("Open Success!");
FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
StreamWriter sw = new StreamWriter(fs);
}
}
const int GENERIC_WRITE = 0x40000000;
const int OPEN_EXISTING = 3;[DllImport("kernel32.dll")
public static extern IntPtr CreateFileA(string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
private void Button1_Click(Object sender, EventArgs e)
{
IntPtr iHandle = CreateFileA("LPT1", GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if(iHandle.ToInt32() == -1)
{
MessageBox.Show("can not open port");
}
else
{
MessageBox.Show("Open Success!");
FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
StreamWriter sw = new StreamWriter(fs);
}
}
解决方案 »
- DataTable的值某列值根据判断后另外一种显示形式绑定到DataGridView中
- C#中关于退出整个应用程序的问题!
- 简单SQL语句
- TreeView 节点显示多行文字,愚翁进来帮忙,谢!
- C#程序安装必须要求和相应的.NET framework版本对应吗?
- 请朋友对用.NET开发winform软件的看法
- 求助:为什么只返回了一人值呢?
- 第一次来 提个安装的问题吧
- 能不能这样访问property?
- 请问到哪里去下载关于C#的书?
- c# 中调用EnumJobs时出问题,有一个参数值返回不回来,有代码,帮忙看看!
- web中能否对DataSet进行for each循环,如果能,怎样写,紧急求助,在线等!!
const GENERIC_WRITE = &H40000000;
const OPEN_EXISTING = 3;
public [DllImport("GAIS", SetLastError=true)] static extern IntPtr CreateFile Lib "kernel32" Alias "CreateFileA" ( string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile) {
private void Button1_Click( System.object sender, System.EventArgs e) Button1.Click {
IntPtr iHandle;
iHandle = CreateFile("LPT1", GENERIC_READ || GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if ((iHandle.ToInt32 = -1) ) {
MsgBox("can not open port");
} else {
MsgBox("Open Succes!");
FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs) //读数据;
StreamWriter sw = new StreamWriter(fs) //些数据;
}
}