C#读取剪贴板内容并把内容记录在XML文件中

本文介绍了一个简单的C#程序,该程序能够定时读取剪贴板中的文本内容,并将其保存到XML文件中。通过设置时间间隔,用户可以自定义读取频率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做一个练习,就是用C# 2005写一个读取剪贴板的程序,并把读取的内容写进XML文件中!先和大家分享一下:
程序运界面如下:


xml文件如下:

程序代码如下:

因为是比较简单的代码,在这里就不做解释了!程序写得很烂,望高手们指点!
http://nicnet.cnblogs.com/archive/2006/02/28/339899.html
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  System.Xml;

namespace  WindowsApplication1
{
    
public partial class Form2 : Form
    
{
        
public Form2()
        
{
            InitializeComponent();
        }

        
private void timer1_Tick(object sender, EventArgs e)
        
{
            XmlDocument xmlDoc 
= new XmlDocument();
            xmlDoc.Load(
"Control.xml");
            XmlNode root 
= xmlDoc.SelectSingleNode("monitor");
            XmlElement xe1 
= xmlDoc.CreateElement("state");
            XmlElement xesub1 
= xmlDoc.CreateElement("time");
            xesub1.InnerText 
= DateTime.Now.ToString();
            xe1.AppendChild(xesub1);
            XmlElement xesub2 
= xmlDoc.CreateElement("result");
            IDataObject iData 
= Clipboard.GetDataObject();
            
if (iData.GetDataPresent(DataFormats.Text))
            
{
                xesub2.InnerText 
= (string)iData.GetData(DataFormats.Text);
                xe1.AppendChild(xesub2);
                root.AppendChild(xe1);
                xmlDoc.Save(
"Control.xml");
            }

            
else
            
{
                MessageBox.Show(
"剪贴板数据不是文本格式,请检查!""错误");
                
this.Close();
            }

        }


        
private void button2_Click(object sender, EventArgs e)
        
{
            
this.Close();
        }


        
private void button1_Click_1(object sender, EventArgs e)
        
{
            
try
            
{
                
if (textBox1.Text != "" && int.Parse(textBox1.Text) != 0)
                
{
                    
this.timer1.Interval = int.Parse(this.textBox1.Text);
                }

            }

            
catch
            
{
                MessageBox.Show(
"请输入数字格式!");
            }

            
finally 
            
{
            }

           
        }

    }

}
<? xml version="1.0" encoding="utf-8"  ?>
< monitor >
  
< state >
    
< time > 2006-02-27 17:00 </ time >
    
< result > 001 </ result >
  
</ state >
</ monitor >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值