C#实验代码Word下载.docx
- 文档编号:5779142
- 上传时间:2023-05-05
- 格式:DOCX
- 页数:35
- 大小:182.36KB
C#实验代码Word下载.docx
《C#实验代码Word下载.docx》由会员分享,可在线阅读,更多相关《C#实验代码Word下载.docx(35页珍藏版)》请在冰点文库上搜索。
usingSystem.Net.NetworkInformation;
//此实验只添加usingSystem.Net.NetworkInformation;
命名空间
namespace实验一
{
///<
summary>
///MainWindow.xaml的交互逻辑
/summary>
publicpartialclassMainWindow:
Window
{
publicMainWindow()
InitializeComponent();
}
privatevoidButton1_Click(objectsender,RoutedEventArgse)
listBox1.Items.Clear();
Stringhost=textBox1.Text;
Pingpingsender=newPing();
try
PingReplyreply=pingsender.Send(host);
if(reply.Status==IPStatus.Success)
listBox1.Items.Add("
目的主机:
"
+reply.Address.ToString());
往返时间:
+reply.RoundtripTime+"
ms"
);
else
失败!
catch(Exceptionex)
MessageBox.Show(ex.Message);
}
实验二:
ipconfig
ListBox命名为listBox1,查询适配器为Button,命名为button1.生成click事件。
usingSystem.Net;
//此实验添加的命名空间为usingSystem.Net.NetworkInformation;
namespace实验二
privatevoidbutton1_Click(objectsender,RoutedEventArgse)
NetworkInterface[]spq=NetworkInterface.GetAllNetworkInterfaces();
intn=1;
一共"
+spq.Length+"
个网络适配器"
foreach(NetworkInterfacespq1inspq)
-----------第"
+n+"
个适配器信息"
适配器信息:
+spq1.Description);
适配器名称:
+spq1.Name);
适配器类型:
+spq1.NetworkInterfaceType);
速度:
+spq1.Speed/1024/1024+"
M"
byte[]wl=spq1.GetPhysicalAddress().GetAddressBytes();
MAC地址:
+BitConverter.ToString(wl));
IPInterfacePropertiessip=spq1.GetIPProperties();
GatewayIPAddressInformationCollectiongips=sip.GatewayAddresses;
网关:
foreach(GatewayIPAddressInformationgipingips)
listBox1.Items.Add(gip.Address);
UnicastIPAddressInformationCollectionuips=sip.UnicastAddresses;
foreach(UnicastIPAddressInformationuipinuips)
IP地址:
+uip.Address);
子网掩码:
+uip.IPv4Mask);
IPAddressCollectionips=sip.DnsAddresses;
foreach(IPAddressipinips)
DNS:
+ip.ToString());
n++;
实验三:
域名解析
添加控件textbox命名textBox1添加button控件命名button1
添加listbox命名listBox1
button1添加click事件创建Window_KeyDown方法
namespace域名解析
///Window1.xaml的交互逻辑
publicpartialclassWindow1:
IPHostEntryipHost=Dns.GetHostEntry(textBox1.Text);
IPAddress[]ips=ipHost.AddressList;
主机名:
+ipHost.HostName);
Ip地址有:
listBox1.Items.Add(ip.ToString());
string[]strs=ipHost.Aliases;
foreach(stringstrinstrs)
listBox1.Items.Add(str);
listBox1.Items.Add(ex.Message);
}
privatevoidWindow_KeyDown(objectsender,KeyEventArgse)
if(e.Key==Key.Enter)
this.button1_Click(sender,e);
实验四:
多线程
TextBlock命名为textBlock1,启动A线程、启动B线程、停止线程都是Button,分别命名为button1、button2、button3,且都生成click事件。
usingSystem.Threading;
此实验的命名空间为usingSystem.Threading;
namespace实验四
publicvolatileboolisStopped;
button1.IsEnabled=true;
button2.IsEnabled=true;
button3.IsEnabled=false;
privatevoidMethod(objectobj)
stringstr=objasstring;
while(!
isStopped)
Thread.Sleep(100);
textBlock1.Dispatcher.Invoke(newShowDelegate(Show),str);
publicdelegatevoidShowDelegate(stringstr);
publicvoidShow(stringstr)
textBlock1.Text+=str;
Threadt=newThread(Method);
t.IsBackground=true;
isStopped=false;
t.Priority=ThreadPriority.AboveNormal;
t.Start("
A"
button1.IsEnabled=false;
button3.IsEnabled=true;
privatevoidbutton2_Click(objectsender,RoutedEventArgse)
B"
button2.IsEnabled=false;
privatevoidbutton3_Click(objectsender,RoutedEventArgse)
isStopped=true;
实验五:
编码解码
(一)
编码类型:
为Label,命名label1,其后为ComboBox,命名为ComboBox1,原始输入:
、编码:
和解码:
都为Labell,分别命名为label2,label3和label4。
其后都为TextBox,命名分别为textBox1、textBox2、textBox3。
开始为Button,命名为button1,生成click事件,还要生成WindowLoaded事件。
namespace实验五
privatevoidWindow_Loaded(objectsender,RoutedEventArgse)
foreach(EncodingInfoeiinEncoding.GetEncodings())
Encodingen=ei.GetEncoding();
ComboBox1.Items.Add(ei.Name);
ComboBox1.SelectedItem="
gb2312"
;
textBox2.Clear();
textBox3.Clear();
stringmessasge=textbox1.Text;
Encodingen=Encoding.GetEncoding(ComboBox1.SelectedItem.ToString());
byte[]bs=en.GetBytes(messasge);
foreach(bytebinbs)
textBox2.Text+=Convert.ToString(b,2);
textBox3.Text=messasge;
编码和解码
(二)
privatevoidEncodeDecode(strings,Encodingencoding)
byte[]bytes=encoding.GetBytes(s);
stringstr=encoding.GetString(bytes);
stringencodeResult=BitConverter.ToString(bytes);
textBox2.Text=string.Format("
编码名称:
{0},\t编码结果:
{1}"
encoding.EncodingName,encodeResult);
textBox3.Text=string.Format(str);
strings=textBox1.Text;
if(textBox1.Text=="
)
MessageBox.Show("
请输入原始信息"
if(ComboBox1.SelectedIndex==-1)
请选择编码类型"
stringcodeType=this.ComboBox1.SelectedItem.ToString();
EncodeDecode(s,Encoding.GetEncoding(codeType));
}
实验六:
异步编程
ScrollViewer命名为ScrollViewer,在ScrollViewer中添加StackPanel控件,然后再对其中添加TextBlock并命名为TextBlock;
启动任务为Button,命名为button1,停止任务为Button,命名为button2。
Button1和button2都生成click事件。
然后再对实验六中添加MyTasks类,方法为:
右键点击实验六-添加-类-将类命名为MyTasks,然后写代码。
usingSystem.Wind
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 实验 代码
![提示](https://static.bingdoc.com/images/bang_tan.gif)