Запись в файл - C# (182275)
Формулировка задачи:
Имеется вот такая программка. Не могу сделать чтобы полученные данные отправлялись в файл.txt.
Получается что в файл должны попадать все значения из буфера, а у меня попадает только последнее. Как реализовать?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace ClipboardMonitor
{
public partial class MainForm : Form
{
//Register a window handle as a clipboard viewer
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetClipboardViewer(IntPtr hWnd);
//Remove a window handle from the clipboard viewers chain
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool ChangeClipboardChain(
IntPtr hWndRemove, // handle to window to remove
IntPtr hWndNewNext // handle to next window
);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
public MainForm()
{
InitializeComponent();
}
private void Output(string message)
{
tbOutput.Text += message + Environment.NewLine;
tbOutput.SelectionStart = tbOutput.Text.Length;
tbOutput.ScrollToCaret();
}
//The messages we shall need to monitor
private const int WM_DRAWCLIPBOARD = 0x0308;
private const int WM_CHANGECBCHAIN = 0x030D;
//A handle of the next clipboard viewer we should send the message to
private IntPtr nextClipboardViewer;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_DRAWCLIPBOARD:
//the contents of the clipboard have changed
{
//Process clipboard change
ClipboardChanged();
//Send the message to the next window
SendMessage(nextClipboardViewer, WM_DRAWCLIPBOARD, IntPtr.Zero, IntPtr.Zero);
break;
}
case WM_CHANGECBCHAIN:
//the clipboard chain has changed and we have to pass the news along
{
if (m.WParam == nextClipboardViewer)
{
//the window we've been passing WM_DRAWCLIPBOARD to has been removed
//from the chain, so we have to update our message target
nextClipboardViewer = m.LParam;
}
else
{
//just pass along the message
SendMessage(nextClipboardViewer, WM_CHANGECBCHAIN, m.WParam, m.LParam);
}
m.Result = IntPtr.Zero;
break;
}
default:
{
base.WndProc(ref m);
break;
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
nextClipboardViewer = SetClipboardViewer(this.Handle);
//Output("Программа начала работу");
}
private void btnStop_Click(object sender, EventArgs e)
{
ChangeClipboardChain(this.Handle, nextClipboardViewer);
Output("Программа закончила работу");
}
private void ClipboardChanged()
{
Output(" " );
Output("Буфер был изменен: "+ DateTime.Now);
string s = "Полученные данные: ";
if (Clipboard.ContainsText())
{
s += Clipboard.GetText();
}
else
{
s += "Нет текста";
}
Output(s);
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void tbOutput_TextChanged(object sender, EventArgs e)
{
}
}
}Решение задачи: «Запись в файл»
textual
Листинг программы
using (var sw = File.AppendText("output.txt"))
{
sw.WriteLine(message + Environment.NewLine);
}