namespace Microsoft.Samples.WinForms.Cs.SimplePad { using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.IO;
using System.ComponentModel;
using System.Windows.Forms; public class SimplePad : System.Windows.Forms.Form {
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components;
protected internal System.Windows.Forms.MenuItem menuItem22;
protected internal System.Windows.Forms.MenuItem selectAllMenu;
protected internal System.Windows.Forms.MenuItem menuItem20;
protected internal System.Windows.Forms.MenuItem gotoMenu;
protected internal System.Windows.Forms.MenuItem menuItem18;
protected internal System.Windows.Forms.MenuItem menuItem17;
protected internal System.Windows.Forms.MenuItem menuItem16;
protected internal System.Windows.Forms.MenuItem menuItem15;
protected internal System.Windows.Forms.MenuItem DeleteMenu;
protected internal System.Windows.Forms.MenuItem pasteMenu;
protected internal System.Windows.Forms.MenuItem copyMenu;
protected internal System.Windows.Forms.MenuItem CutMenu;
protected internal System.Windows.Forms.MenuItem menuItem8;
protected internal System.Windows.Forms.MenuItem UndoMenu;
protected internal System.Windows.Forms.MenuItem optionsMenu;
protected internal System.Windows.Forms.MenuItem menuItem7;
protected internal System.Windows.Forms.MenuItem fontMenu;
protected internal System.Windows.Forms.MenuItem wordWrapMenu;
protected internal System.Windows.Forms.PrintDialog printDialog1;
protected internal System.Windows.Forms.FontDialog fontDialog1;
protected internal System.Windows.Forms.SaveFileDialog saveFileDialog1;
protected internal System.Windows.Forms.OpenFileDialog openFileDialog1;
protected internal System.Windows.Forms.StatusBarPanel linePanel;
protected internal System.Windows.Forms.StatusBarPanel statusPanel;
protected internal System.Windows.Forms.StatusBar statusBar1;
protected internal System.Windows.Forms.RichTextBox textArea;
protected internal System.Windows.Forms.MenuItem exitMenu;
protected internal System.Windows.Forms.MenuItem menuItem12;
protected internal System.Windows.Forms.MenuItem printMenu;
protected internal System.Windows.Forms.MenuItem pageSetupMenu;
protected internal System.Windows.Forms.MenuItem menuItem9;
protected internal System.Windows.Forms.MenuItem saveAsMenu;
protected internal System.Windows.Forms.MenuItem saveMenu;
protected internal System.Windows.Forms.MenuItem openMenu;
protected internal System.Windows.Forms.MenuItem newMenu;
protected internal System.Windows.Forms.MenuItem menuItem4;
protected internal System.Windows.Forms.MenuItem menuItem3;
protected internal System.Windows.Forms.MenuItem menuItem2;
protected internal System.Windows.Forms.MenuItem menuItem1;
protected internal System.Windows.Forms.MainMenu mainMenu;
protected internal System.Windows.Forms.MenuItem printPreviewMenu;
protected internal System.IO.FileSystemWatcher dirWatcher; private static readonly string noFilename = "无标题";
private static readonly string notDirtyCaptionFormat = "{0} - SimplePad+";
private static readonly string dirtyCaptionFormat = "{0}* - SimplePad+";
private string editingFileName = null;
private bool dirty = false;
private bool fileOnDiskModified = false;
private PageSettings storedPageSettings = null ;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.IO;
using System.ComponentModel;
using System.Windows.Forms; public class SimplePad : System.Windows.Forms.Form {
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components;
protected internal System.Windows.Forms.MenuItem menuItem22;
protected internal System.Windows.Forms.MenuItem selectAllMenu;
protected internal System.Windows.Forms.MenuItem menuItem20;
protected internal System.Windows.Forms.MenuItem gotoMenu;
protected internal System.Windows.Forms.MenuItem menuItem18;
protected internal System.Windows.Forms.MenuItem menuItem17;
protected internal System.Windows.Forms.MenuItem menuItem16;
protected internal System.Windows.Forms.MenuItem menuItem15;
protected internal System.Windows.Forms.MenuItem DeleteMenu;
protected internal System.Windows.Forms.MenuItem pasteMenu;
protected internal System.Windows.Forms.MenuItem copyMenu;
protected internal System.Windows.Forms.MenuItem CutMenu;
protected internal System.Windows.Forms.MenuItem menuItem8;
protected internal System.Windows.Forms.MenuItem UndoMenu;
protected internal System.Windows.Forms.MenuItem optionsMenu;
protected internal System.Windows.Forms.MenuItem menuItem7;
protected internal System.Windows.Forms.MenuItem fontMenu;
protected internal System.Windows.Forms.MenuItem wordWrapMenu;
protected internal System.Windows.Forms.PrintDialog printDialog1;
protected internal System.Windows.Forms.FontDialog fontDialog1;
protected internal System.Windows.Forms.SaveFileDialog saveFileDialog1;
protected internal System.Windows.Forms.OpenFileDialog openFileDialog1;
protected internal System.Windows.Forms.StatusBarPanel linePanel;
protected internal System.Windows.Forms.StatusBarPanel statusPanel;
protected internal System.Windows.Forms.StatusBar statusBar1;
protected internal System.Windows.Forms.RichTextBox textArea;
protected internal System.Windows.Forms.MenuItem exitMenu;
protected internal System.Windows.Forms.MenuItem menuItem12;
protected internal System.Windows.Forms.MenuItem printMenu;
protected internal System.Windows.Forms.MenuItem pageSetupMenu;
protected internal System.Windows.Forms.MenuItem menuItem9;
protected internal System.Windows.Forms.MenuItem saveAsMenu;
protected internal System.Windows.Forms.MenuItem saveMenu;
protected internal System.Windows.Forms.MenuItem openMenu;
protected internal System.Windows.Forms.MenuItem newMenu;
protected internal System.Windows.Forms.MenuItem menuItem4;
protected internal System.Windows.Forms.MenuItem menuItem3;
protected internal System.Windows.Forms.MenuItem menuItem2;
protected internal System.Windows.Forms.MenuItem menuItem1;
protected internal System.Windows.Forms.MainMenu mainMenu;
protected internal System.Windows.Forms.MenuItem printPreviewMenu;
protected internal System.IO.FileSystemWatcher dirWatcher; private static readonly string noFilename = "无标题";
private static readonly string notDirtyCaptionFormat = "{0} - SimplePad+";
private static readonly string dirtyCaptionFormat = "{0}* - SimplePad+";
private string editingFileName = null;
private bool dirty = false;
private bool fileOnDiskModified = false;
private PageSettings storedPageSettings = null ;
FileStream file = File.OpenRead(filename);
int n = file.Read(buffer,0,file.Length); //buffer ,即是你要的FileStream target = File.Open(sFileName,FileMode.OpenOrCreate,FileAccess.Write,FileShare.None );
target.Write(buffer,0,n); //就可以完成写入//别忘了关闭附:一个上传文件的例子(没有整理,没有兴趣看上面就够了,哈哈):
============================================================
/// <summary>
/// 上传文件
/// </summary>
/// <param name="filename">要上传的文件名</param>
public UploadException SendFile(string filename,string targetname)
{
//send : CMD(WORD = 0x0301)+filename's length(WORD)+filename(target)+size(DWORD)+times(WORD),
//Receive: CMD(WORD = 0x0302)+reday status(BYTE 1= OK,2 = FAIL)
if(!m_Socket.Connected )
return UploadException.CONNECT_FAILED; //return value
UploadException re = UploadException.OK;
//file
FileStream file = null;
//file size
int size ;
//Upload times
int times ; int nTimes = 0;
try // try to open the file
{
file = File.OpenRead(filename); size = (int)file.Length; times = size/(ONCE_COUNT -2); m_byte.Reset(); int nFileNameLength = m_asen.GetByteCount(targetname); int pos = 0; //put data position m_byte.SetWord(0x0302,ref pos);
m_byte.SetWord((short)nFileNameLength,ref pos);
m_byte.SetByte(m_asen.GetBytes(targetname),ref pos);
m_byte.SetDword(size,ref pos);
m_byte.SetWord((short)times,ref pos);
byte[] bSend = m_byte.GetBuffer(); // m_Socket.BeginSend(bSend,0,bSend.Length,SocketFlags.None,proSend,m_Socket);//Send(bSend);
//等待回应
while(m_nPut == 0 && nTimes < 100)
{
// if( 0 != m_Socket.Receive(bSend))
// m_nPut = 2;
System.Threading.Thread.Sleep(200) ;
nTimes ++; }
if(m_nPut == 2)
return UploadException.OTHER; //组织发送包
for(int i =0; i< times; i++)
{
pos = 0;
m_byte.SetWord(bSend,0x0303,ref pos); //cmd
int n = file.Read(bSend,i*(ONCE_COUNT -2 ) + 2,ONCE_COUNT -2 );
m_Socket.Send(bSend,n + 2, SocketFlags.None );
// m_Socket.BeginSend(bSend,0,n,SocketFlags.None,proSend,m_Socket); }
}
catch(SocketException) // 访问 Socket 时发生操作系统错误。
{
re = UploadException.CONNECT_FAILED;
}
catch(ObjectDisposedException) // Socket 已关闭。
{
re = UploadException.CONNECT_FAILED;
}
服务端
============================================================
/// <summary>
/// 接收数据,并处理
/// </summary>
/// <param name="asyn"></param>
public void OnDataReceived(IAsyncResult asyn)
{
//send : CMD(WORD = 0x0301)+filename's length(WORD)+filename(target)+size(DWORD)+times(WORD),
//Receive: CMD(WORD = 0x0302)+reday status(BYTE 1= OK,2 = FAIL) //check
if( !((asyn.AsyncState) is int))
return; string sFileName = null;
short nNameLength = 0;
int nSize = 0;
byte byOper = ALL_RIGHT; //打开文件操作结果代码 int nIndex = (int)asyn.AsyncState; if(nIndex >= MAX_CONNECT || nIndex <0 )
return ;
TSocketPacket theSockId = aSock[nIndex] ; if(theSockId == null)
return ; try
{
//end receive...
int iRx = theSockId.thisSocket.EndReceive (asyn);
// Console.WriteLine("Received Data From {0},length = {1}",theSockId.thisSocket.RemoteEndPoint.ToString(),iRx);
int pos = 0; int nCmd = m_byte.GetWord(theSockId.dataBuffer,ref pos); if(nCmd == CMD_C_REQUERY) //requery
{ nNameLength = m_byte.GetWord(theSockId.dataBuffer,ref pos); sFileName = m_ascn.GetString(theSockId.dataBuffer,pos,nNameLength); pos += nNameLength; nSize = m_byte.GetDword(theSockId.dataBuffer,ref pos); theSockId.nTimes= m_byte.GetDword(theSockId.dataBuffer,ref pos); Console.WriteLine("Get Requery file = '{0}',size = {1} ,times= {2}",sFileName,nSize,theSockId.nTimes); // assert m_file not opened
if (theSockId.m_file != null)
//return ;
byOper = FILE_ALWAYS_OPENED; try //open the file
{ theSockId.m_file = File.Open(sFileName,FileMode.OpenOrCreate,FileAccess.Write,FileShare.None ); Console.WriteLine("Open or Create file '{0}'",sFileName); byOper = ALL_RIGHT; theSockId.nCurTime = 0; } catch(Exception e)
{
Console.WriteLine(e.Message+","+sFileName);
byOper = OPEN_FILE_FAILED;
} //组织回应命令
pos = 0; m_byte.SetWord(bSend,CMD_S_REQUERY,ref pos); m_byte.SetByte(bSend,new byte[]{byOper},ref pos);
theSockId.thisSocket.Send(bSend,0,pos,SocketFlags.None );
}
else if(nCmd == CMD_C_DETAIL) //detail
{
// Console.WriteLine("Recevied file detail info {0} times",nCurTime);
// assert m_file is opened
if(theSockId.m_file == null)
return ;
//close
theSockId.nCurTime = m_byte.GetDword(theSockId.dataBuffer,ref pos); theSockId.m_file.Write(theSockId.dataBuffer, pos,iRx - pos); //每十个包保存一次
if((theSockId.nCurTime /10 ) == 0)
//save
theSockId.m_file.Flush(); //组织回应命令
pos = 0; m_byte.SetWord(bSend,CMD_S_DETAIL,ref pos);
m_byte.SetDword(bSend,theSockId.nCurTime,ref pos);
theSockId.thisSocket.Send(bSend,0,pos,SocketFlags.None); if( theSockId.nCurTime == theSockId.nTimes )
{
//save
theSockId.m_file.Flush();
theSockId.m_file.Close();
theSockId.m_file = null;
theSockId.nTimes = 0;
// sFileName= null;
} }
WaitForData(nIndex ); }
谢谢qieyj(温馨港湾)