📄 clientform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using FileInterface;
using System.IO;
using System.Threading;
namespace FileClient
{
public partial class ClientForm : Form
{
IFileInterface _proxy;
public ClientForm()
{
InitializeComponent();
}
private void btnAction_Click(object sender, EventArgs e)
{
if (_proxy == null)
{
try
{
NetTcpBinding binding = new NetTcpBinding();
binding.TransferMode = TransferMode.Streamed;
binding.SendTimeout = new TimeSpan(0, 30, 0);
_proxy = ChannelFactory<IFileInterface>.CreateChannel(binding, new
EndpointAddress(textBox_ServiceAddr.Text));
IContextChannel obj = _proxy as IContextChannel;
string s = obj.SessionId;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
btnAction.Text = "停止";
}
else
{
_proxy = null;
btnAction.Text = "连接";
}
}
private void btnSend_Click(object sender, EventArgs e)
{
_proxy.Test(this.textBoxMsg.Text);
}
private string GetSendFile()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
// openFileDialog.InitialDirectory = "c:\\";//注意这里写路径时要用c:\\而不是c:\
openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
return openFileDialog.FileName;
}
return string.Empty;
}
private void btnFileSend_Click(object sender, EventArgs e)
{
string filePath;
filePath = GetSendFile();
if (filePath == string.Empty)
{
return;
}
if (_proxy == null)
return;
FileUploadMessage file = null;
try
{
file = new FileUploadMessage();
file.SavePath = textBoxMsg.Text;
file.FileName = Path.GetFileName(filePath);
file.FileData = new FileStream(filePath, FileMode.Open);
IContextChannel obj = _proxy as IContextChannel;
string s = obj.SessionId;
//
FileSendThread sendThread = new FileSendThread();
sendThread._file = file;
sendThread._proxy = _proxy;
Thread threadRead = new Thread(new ThreadStart(sendThread.SendFile));
threadRead.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ClientForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (_proxy != null)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
string filePath;
filePath = GetSendFile();
if (filePath == string.Empty)
{
return;
}
FileStream fs = null;
try
{
fs = new FileStream(filePath, FileMode.Open);
MessageBox.Show("new FileStream");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
}
}
}
class FileSendThread
{
public FileUploadMessage _file;
public IFileInterface _proxy;
public void SendFile()
{
try
{
_proxy.UploadFile(_file);
}
catch (Exception ex)
{
}
finally
{
if (_file.FileData != null)
{
_file.FileData.Close();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -