用得着的C# FAQ

Q:如何在XMl文档中某个节点位置加入一些节点,不是整个文件写

A:XmlDocument xmldoc = new XmlDocument();

xmlDoc.Load( filePath );

XmlNode root = xmlDoc.DocumentElement.SelectSingleNode( xPath );

XmlNode newElem=xmlDoc.CreateNode(XmlNodeType.Element, "pages", "");

root.AppendChild( newElem );

xmlDoc.Save(filePath);

---------------------------------------------------------------

TextWriter tw=new StreamWriter(@"C:\csharp\06\iverson.xml");

XmlTextWriter writer=new XmlTextWriter(tw);

writer.WriteStartElement("Human");

writer.WriteComment("this is test sample");

writer.WriteStartElement("Name");

writer.WriteString("Iverson");

writer.WriteEndElement();

writer.WriteElementString("tel","13020024783");

writer.WriteStartElement("Jobs");

writer.WriteStartAttribute("Job1",null);

writer.WriteString("Author");

writer.WriteEndAttribute();

writer.WriteStartAttribute("Job2",null);

writer.WriteString("Teacher");

writer.WriteEndAttribute();

writer.WriteEndElement();

writer.WriteEndElement();

writer.Close();

Q:c#开发winform,怎么去执行一个外部的exe文件?

象vb的shell那样!

A: 单纯的执行比较简单:

Process myProcess = new Process();

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();

myProcessStartInfo.FileName = "Netpad";

myProcess.StartInfo = myProcessStartInfo;

myProcess.Start();

但我的问题就比较麻烦了了

---------------------------------------------------------------

using System.Diagnostics;

Process.Start("外部的exe文件");

例如:

try

{

Process.Start("regedit");//启动注册表编辑器

}

catch

{

MessageBox.Show("程序启动出错");

}

---------------------------------------------------------------

Process.Start("外部的exe文件");

打开一个进程就好了

Q:我想在两个程序间交换数据,该如何实现?

A:方法1:

通过数据库

方法二:

配置文件

如果是webform的话:可以用Session

wish u good luck

Greatsft

---------------------------------------------------------------

两种方法,一种是把数据写入到文件,另一个程序去读!

第二种方法,用socket,自己定义一个通道,相互间通讯,就好像通过网络通讯一样,但其实是在同一台电脑上!前提是要有Tcp/Ip环境!而且比较适合对数据及时性要求较高的。

以上两种方法都是比较简单的,我以为。

最好是去看看有没有可能用全局Dll调用的方法,进行进程间的共享!不过本人目前还没有这方面的经验!

---------------------------------------------------------------

如果两个程序之间只是数据通信,可以采用以下方法:

1、通过共享文件或数据库

2、通过管道

3、通过邮槽

4、通过Socket

5、其它

如果两个程序之间不仅是数据通信,而且是处理通信(即程序A可以调用程序B的方法),可以采用以下方法:

1、Dcom

2、.net Remoting

3、Web Service

---------------------------------------------------------------

进程之间通讯的几种方法:

在Windows程序中,各个进程之间常常需要交换数据,进行数据通讯。常用的方法有

使用内存映射文件

通过共享内存DLL共享内存

使用SendMessage向另一进程发送WM_COPYDATA消息

比起前两种的复杂实现来,WM_COPYDATA消息无疑是一种经济实惠的一中方法.

WM_COPYDATA消息的主要目的是允许在进程间传递只读数据。Windows在通过WM_COPYDATA消息传递期间,不提供继承同步方式。SDK文档推荐用户使用SendMessage函数,接受方在数据拷贝完成前不返回,这样发送方就不可能删除和修改数据:

这个函数的原型及其要用到的结构如下:

SendMessage(hwnd,WM_COPYDATA,wParam,lParam);

其中,WM_COPYDATA对应的十六进制数为0x004A

wParam设置为包含数据的窗口的句柄。lParam指向一个COPYDATASTRUCT的结构:

typedef struct tagCOPYDATASTRUCT{

DWORD dwData;//用户定义数据

DWORD cbData;//数据大小

PVOID lpData;//指向数据的指针

}COPYDATASTRUCT;

该结构用来定义用户数据。

具体过程如下:

首先,在发送方,用FindWindow找到接受方的句柄,然后向接受方发送WM_COPYDATA消息.

接受方在DefWndProc事件中,来处理这条消息.由于中文编码是两个字节,所以传递中文时候字节长度要搞清楚.

体代码如下:

//---------------------------------------------------

//发送方:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Runtime.InteropServices;

namespace WinFormSendMsg

{

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.Button button1;

private System.ComponentModel.Container components = null;

const int WM_COPYDATA = 0x004A;

public Form1()

{

InitializeComponent();

}

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

private void InitializeComponent()

{

this.textBox1 = new System.Windows.Forms.TextBox();

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(184, 24);

this.textBox1.Name = "textBox1";

this.textBox1.Size = new System.Drawing.Size(128, 21);

this.textBox1.TabIndex = 0;

this.textBox1.Text = "textBox1";

//

// button1

//

this.button1.Location = new System.Drawing.Point(344, 16);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(112, 32);

this.button1.TabIndex = 1;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(536, 142);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button1,

this.textBox1});

this.Name = "Form1";

this.Text = "发送方窗体";

this.ResumeLayout(false);

}

static void Main()

{

Application.Run(new Form1());

}

[DllImport("User32.dll",EntryPoint="SendMessage")]

private static extern int SendMessage(

int hWnd, // handle to destination window

int Msg, // message

int wParam, // first message parameter

ref COPYDATASTRUCT lParam // second message parameter

);

[DllImport("User32.dll",EntryPoint="FindWindow")]

private static extern int FindWindow(string lpClassName,string

lpWindowName);

private void button1_Click(object sender, System.EventArgs e)

{

int WINDOW_HANDLER = FindWindow(null,@"接收方窗体");

if(WINDOW_HANDLER == 0)

{

}

else

{

byte[] sarr = System.Text.Encoding.Default.GetBytes(this.textBox1.Text);

int len = sarr.Length;

COPYDATASTRUCT cds;

cds.dwData = (IntPtr) 100;

cds.lpData = this.textBox1.Text;

cds.cbData = len + 1;

SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds);

}

}

}

public struct COPYDATASTRUCT

{

public IntPtr dwData;

public int cbData;

[MarshalAs(UnmanagedType.LPStr)] public string lpData;

}

}

//---------------------------------------------------

//接受方

//---------------------------------------------------

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Runtime.InteropServices;

namespace WindowsFormGetMsg

{

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.TextBox textBox1;

private System.ComponentModel.Container components = null;

const int WM_COPYDATA = 0x004A;

public Form1()

{

InitializeComponent();

}

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

private void InitializeComponent()

{

this.textBox1 = new System.Windows.Forms.TextBox();

this.SuspendLayout();

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(176, 32);

this.textBox1.Name = "textBox1";

this.textBox1.Size = new System.Drawing.Size(160, 21);

this.textBox1.TabIndex = 0;

this.textBox1.Text = "textBox1";

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(432, 266);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.textBox1});

this.Name = "Form1";

this.Text = "接收方窗体";

this.ResumeLayout(false);

}

static void Main()

{

Application.Run(new Form1());

}

protected override void DefWndProc(ref System.Windows.Forms.Message m)

{

switch(m.Msg)

{

case WM_COPYDATA:

COPYDATASTRUCT mystr = new COPYDATASTRUCT();

Type mytype = mystr.GetType();

mystr =(COPYDATASTRUCT)m.GetLParam(mytype);

this.textBox1.Text =mystr.lpData;

break;

default:

base.DefWndProc(ref m);

break;

}

}

}

public struct COPYDATASTRUCT

{

public IntPtr dwData;

public int cbData;

[MarshalAs(UnmanagedType.LPStr)] public string lpData;

}

}

将2进制数组转换成图片文件。

转换成2进制数组

public static byte[] GetFileBytes(string Filename)

{

if(Filename == "")

return null;

FileStream fileStream = new FileStream(Filename,FileMode.Open,FileAccess.Read);

BinaryReader binaryReader = new BinaryReader(fileStream);

byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length);

binaryReader.Close();

fileStream.Close();

return fileBytes;

}

将2进制数组转换成文件

FileStream fileStream = new FileStream("文件路径",FileMode.OpenOrCreate,FileAccess.Write);

BinaryWriter binaryWriter = new BinaryWriter(fileStream);

binaryWriter.Write(fileBytes);

binaryWriter.Flush();

binaryWriter.Close();

fileStream.Close();