可用StreamWriter类﹕ FileStram = fs = new FileStream(@”C:\MyDocuments\ReadMe.txt”,FileMode.CreateNew,FileAssess.Write,FileShare.ShareRead); StreamWriter sw = new StreamWriter(fs);
你可以看看MSDN中关于Clipboard 类的帮助 示例 以下示例使用 Clipboard 方法将数据放置到系统剪贴板上并从其中检索这些数据。此代码假定已经在窗体上放置 button1、button2、textBox1 和 textBox2。button1_Click 方法调用 SetDataObject 来从文本框中提取选定的文本,然后将其放置到系统剪贴板上。button2_Click 方法调用 GetDataObject 来从系统剪贴板中检索数据。此代码使用 IDataObject 和 DataFormats 提取返回的数据并在 textBox2 中显示该数据。private void button1_Click(object sender, System.EventArgs e) { // Takes the selected text from a text box and puts it on the clipboard. if(textBox1.SelectedText != "") Clipboard.SetDataObject(textBox1.SelectedText); else textBox2.Text = "No text selected in textBox1"; }
private void button2_Click(object sender, System.EventArgs e) { // Declares an IDataObject to hold the data returned from the clipboard. // Retrieves the data from the clipboard. IDataObject iData = Clipboard.GetDataObject();
// Determines whether the data is in a format you can use. if(iData.GetDataPresent(DataFormats.Text)) { // Yes it is, so display it in a text box. textBox2.Text = (String)iData.GetData(DataFormats.Text); } else { // No it is not. textBox2.Text = "Could not retrieve data off the clipboard."; } }
//FileStream byte[] fonts = new byte[100]; FileStream fs = new FileStream("c:\\test.txt",FileMode.Create,FileAccess.ReadWrite); fs.Read(fonts,0,100); fs.Write(fonts,0,100);//写 fs.Close();
I think you can call keybd_event function to send the VK_SNAPSHOT key code via PInvoke, and then use the Clipboard class to retrieve the data from the clipboard.public const int VK_SNAPSHOT = 0x2C;[DllImport("user32.dll")] public static extern void keybd_event(byte bVk,byte bScan,int dwFlags,int dwExtraInfo);Hope this helps.
FileStram = fs = new FileStream(@”C:\MyDocuments\ReadMe.txt”,FileMode.CreateNew,FileAssess.Write,FileShare.ShareRead);
StreamWriter sw = new StreamWriter(fs);
示例
以下示例使用 Clipboard 方法将数据放置到系统剪贴板上并从其中检索这些数据。此代码假定已经在窗体上放置 button1、button2、textBox1 和 textBox2。button1_Click 方法调用 SetDataObject 来从文本框中提取选定的文本,然后将其放置到系统剪贴板上。button2_Click 方法调用 GetDataObject 来从系统剪贴板中检索数据。此代码使用 IDataObject 和 DataFormats 提取返回的数据并在 textBox2 中显示该数据。private void button1_Click(object sender, System.EventArgs e) {
// Takes the selected text from a text box and puts it on the clipboard.
if(textBox1.SelectedText != "")
Clipboard.SetDataObject(textBox1.SelectedText);
else
textBox2.Text = "No text selected in textBox1";
}
private void button2_Click(object sender, System.EventArgs e) {
// Declares an IDataObject to hold the data returned from the clipboard.
// Retrieves the data from the clipboard.
IDataObject iData = Clipboard.GetDataObject();
// Determines whether the data is in a format you can use.
if(iData.GetDataPresent(DataFormats.Text)) {
// Yes it is, so display it in a text box.
textBox2.Text = (String)iData.GetData(DataFormats.Text);
}
else {
// No it is not.
textBox2.Text = "Could not retrieve data off the clipboard.";
}
}
byte[] fonts = new byte[100];
FileStream fs = new FileStream("c:\\test.txt",FileMode.Create,FileAccess.ReadWrite);
fs.Read(fonts,0,100);
fs.Write(fonts,0,100);//写
fs.Close();
我记得有几个api函数能解决此类问提的.
via PInvoke, and then use the Clipboard class to retrieve the data from the
clipboard.public const int VK_SNAPSHOT = 0x2C;[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk,byte bScan,int
dwFlags,int dwExtraInfo);Hope this helps.
{
//声明一个saveFileDialog的实体
SaveFileDialog mySaveFileDialog=new SaveFileDialog();
//标题
mySaveFileDialog.Title="保存文件";
//初始时路径
mySaveFileDialog.InitialDirectory="c:\\Documents and Settings\\Administrator\\桌面\\";
//过滤文件格式
mySaveFileDialog.Filter="文本文件(*.txt) | *.txt| 其它文件(*.*) | *.*";
//把保存文件的对话框显示出来
if(mySaveFileDialog.ShowDialog()==DialogResult.OK)
{
//声明流
Stream fileStream;
//打开文件
if ((fileStream=mySaveFileDialog.OpenFile())!=null)
{
//声明writeAdapter为StreamWriter类型
//以将字符串写入文件流中
StreamWriter writeAdapter=null;
writeAdapter=new StreamWriter(fileStream);
try
{
//将数据通过流写入对象写入文件
//写入姓名
writeAdapter.Write("姓名: ");
writeAdapter.WriteLine(NameTB.Text.ToString());
//写入性别
//若Checked为true则写入1,否则写入0
writeAdapter.Write("性别: ");
if(SexManRB.Checked==true)
writeAdapter.WriteLine("男");
else
writeAdapter.WriteLine("女");
//写入学历的索引值
writeAdapter.Write("学历: ");
writeAdapter.WriteLine(AcademicLB.SelectedIndex);
//写入兴趣
//若Checked为true则写入1,否则写入0
writeAdapter.WriteLine("兴趣: ");
if(HabitCB1.Checked==true)
writeAdapter.WriteLine(" 唱歌");
else
writeAdapter.Write("");
if(HabitCB2.Checked==true)
writeAdapter.WriteLine(" 阅读");
else
writeAdapter.Write("");
if(HabitCB3.Checked==true)
writeAdapter.WriteLine(" 旅游");
else
writeAdapter.WriteLine("");
//写入生日
writeAdapter.Write("生日: ");
writeAdapter.WriteLine(BirthdayDTP.Value);
//写入职业
writeAdapter.Write("职业: ");
writeAdapter.WriteLine(JobCOB.Text); //...
//..
}
catch(IOException e)
{
Console.WriteLine(e.ToString());
}
//mySaveFileDialog.ShowDialog();
//最后,将流写入对象关闭
writeAdapter.Close();
//MenuItem saveMenuItem=new MenuItem("保存",new EventHandler(this.SaveFileMenu),Shortcut,CtrlS);
}
}
//Application.Exit();
}
using System;
using System.Runtime.InteropServices;
using System.Text;namespace ConsoleApplication8{
class Class1{
[STAThread]
static void Main(string[] args){
// Display current status of keys.
Console.WriteLine(
"**BEFORE**\r\nCAP: {0}\r\nSCR: {1}\r\nNUM: {2}",
Keyboard.GetState(VirtualKeys.VK_CAPITAL)?"ON":"OFF",
Keyboard.GetState(VirtualKeys.VK_SCROLL)?"ON":"OFF",
Keyboard.GetState(VirtualKeys.VK_NUMLOCK)?"ON":"OFF"
);
//Toggle all the keys:
Keyboard.SetState(
VirtualKeys.VK_CAPITAL,
!Keyboard.GetState(VirtualKeys.VK_CAPITAL)
);
Keyboard.SetState(
VirtualKeys.VK_SCROLL,
!Keyboard.GetState(VirtualKeys.VK_SCROLL)
);
Keyboard.SetState(
VirtualKeys.VK_NUMLOCK,
!Keyboard.GetState(VirtualKeys.VK_NUMLOCK)
);
// Display new status of keys.
Console.WriteLine(
"\r\n**AFTER**\r\nCAP: {0}\r\nSCR: {1}\r\nNUM: {2}",
Keyboard.GetState(VirtualKeys.VK_CAPITAL)?"ON":"OFF",
Keyboard.GetState(VirtualKeys.VK_SCROLL)?"ON":"OFF",
Keyboard.GetState(VirtualKeys.VK_NUMLOCK)?"ON":"OFF"
);
Console.ReadLine();
}
}
public enum VirtualKeys: byte{
VK_NUMLOCK = 0x90,
VK_SCROLL = 0x91,
VK_CAPITAL = 0x14
}
class Keyboard{
const uint KEYEVENTF_EXTENDEDKEY = 0x1;
const uint KEYEVENTF_KEYUP = 0x2;
[DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey);
[DllImport("user32.dll")]
static extern void keybd_event(
byte bVk,
byte bScan,
uint dwFlags,
uint dwExtraInfo
);
public static bool GetState(VirtualKeys Key){
return (GetKeyState((int)Key)==1);
}
public static void SetState(VirtualKeys Key, bool State){
if(State!=GetState(Key)){
keybd_event(
(byte)Key,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
0
);
keybd_event(
(byte)Key,
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0
);
}
}
}
}