用得著的C# FAQ

來源:互聯網
上載者:User

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();

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.