在.net中,怎么知道一个文件正在被写入? thank you 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我做得有个例子..当初我监视安装程序的.呵呵..using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace Example120_响应文件系统事件{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.IO.FileSystemWatcher fileSystemWatcher1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1; private System.Windows.Forms.TextBox txtpath; private System.Windows.Forms.TextBox txt1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.fileSystemWatcher1 = new System.IO.FileSystemWatcher(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.txtpath = new System.Windows.Forms.TextBox(); this.txt1 = new System.Windows.Forms.TextBox(); ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit(); this.SuspendLayout(); // // fileSystemWatcher1 // this.fileSystemWatcher1.EnableRaisingEvents = true; this.fileSystemWatcher1.IncludeSubdirectories = true; this.fileSystemWatcher1.NotifyFilter = ((System.IO.NotifyFilters)((System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName))); this.fileSystemWatcher1.Path = "c:\\"; this.fileSystemWatcher1.SynchronizingObject = this; this.fileSystemWatcher1.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Deleted); this.fileSystemWatcher1.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher1_Renamed); this.fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Created); // // button1 // this.button1.Location = new System.Drawing.Point(392, 8); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(64, 24); this.button1.TabIndex = 4; this.button1.Text = "清除"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(304, 8); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(64, 24); this.button2.TabIndex = 5; this.button2.Text = "监视目录"; this.button2.Click += new System.EventHandler(this.button2_Click); // // folderBrowserDialog1 // this.folderBrowserDialog1.Description = "请选择要监视的目录"; this.folderBrowserDialog1.SelectedPath = "C:\\"; this.folderBrowserDialog1.ShowNewFolderButton = false; // // txtpath // this.txtpath.Location = new System.Drawing.Point(8, 8); this.txtpath.Name = "txtpath"; this.txtpath.ReadOnly = true; this.txtpath.Size = new System.Drawing.Size(288, 21); this.txtpath.TabIndex = 7; this.txtpath.Text = "c:\\"; // // txt1 // this.txt1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.txt1.Location = new System.Drawing.Point(0, 40); this.txt1.Multiline = true; this.txt1.Name = "txt1"; this.txt1.ReadOnly = true; this.txt1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.txt1.Size = new System.Drawing.Size(480, 424); this.txt1.TabIndex = 8; this.txt1.Text = ""; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(480, 462); this.Controls.Add(this.txt1); this.Controls.Add(this.txtpath); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "监视中....."; ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e) { txt1.Text+=e.FullPath.ToString()+ "已添加到你的目录中.\t"; } private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e) { txt1.Text+=e.OldFullPath.ToString() +"已经被重命名为:"+e.Name.ToString()+"\t"; } private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e) { txt1.Text+=e.FullPath.ToString() +"已经被删除.\t"; } private void button1_Click(object sender, System.EventArgs e) { txt1.Text=""; } private void button2_Click(object sender, System.EventArgs e) { folderBrowserDialog1.ShowDialog(); txtpath.Text=folderBrowserDialog1.SelectedPath; fileSystemWatcher1.Path = folderBrowserDialog1.SelectedPath; } }} FileSystemWatcher可以监听到文件变动。想确定某个文件当前是否正在被写入应该是不可能的,但可以通过打开这个文件来判断此文件是否被其它程序占用。 Winform开发,想问一下单词查询的这种效果是怎么实现的? ContextMenuStrip控件不能使用,求高人解决 一个对LIST操作的问题 如何检索XML? 它的输出结果是什么? Web开发中,继承Button的问题 关于网络数据传输问题,请各位给点意见。 求WinForm下,窗体上的控件随窗体缩放而自动缩放的控件 如何用向oralce中插入blob数据 十万火急、在线求助: 曲线的连续、不分页打印问题 在.NETFRAMEWORK1.1中,怎么把函数指针转为代理 子窗体如何向父窗体传值?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Example120_响应文件系统事件
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.IO.FileSystemWatcher fileSystemWatcher1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
private System.Windows.Forms.TextBox txtpath;
private System.Windows.Forms.TextBox txt1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.txtpath = new System.Windows.Forms.TextBox();
this.txt1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
this.SuspendLayout();
//
// fileSystemWatcher1
//
this.fileSystemWatcher1.EnableRaisingEvents = true;
this.fileSystemWatcher1.IncludeSubdirectories = true;
this.fileSystemWatcher1.NotifyFilter = ((System.IO.NotifyFilters)((System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName)));
this.fileSystemWatcher1.Path = "c:\\";
this.fileSystemWatcher1.SynchronizingObject = this;
this.fileSystemWatcher1.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Deleted);
this.fileSystemWatcher1.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher1_Renamed);
this.fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Created);
//
// button1
//
this.button1.Location = new System.Drawing.Point(392, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 4;
this.button1.Text = "清除";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(304, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 24);
this.button2.TabIndex = 5;
this.button2.Text = "监视目录";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// folderBrowserDialog1
//
this.folderBrowserDialog1.Description = "请选择要监视的目录";
this.folderBrowserDialog1.SelectedPath = "C:\\";
this.folderBrowserDialog1.ShowNewFolderButton = false;
//
// txtpath
//
this.txtpath.Location = new System.Drawing.Point(8, 8);
this.txtpath.Name = "txtpath";
this.txtpath.ReadOnly = true;
this.txtpath.Size = new System.Drawing.Size(288, 21);
this.txtpath.TabIndex = 7;
this.txtpath.Text = "c:\\";
//
// txt1
//
this.txt1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txt1.Location = new System.Drawing.Point(0, 40);
this.txt1.Multiline = true;
this.txt1.Name = "txt1";
this.txt1.ReadOnly = true;
this.txt1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txt1.Size = new System.Drawing.Size(480, 424);
this.txt1.TabIndex = 8;
this.txt1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 462);
this.Controls.Add(this.txt1);
this.Controls.Add(this.txtpath);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "监视中.....";
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
txt1.Text+=e.FullPath.ToString()+ "已添加到你的目录中.\t";
} private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
{
txt1.Text+=e.OldFullPath.ToString() +"已经被重命名为:"+e.Name.ToString()+"\t"; } private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
txt1.Text+=e.FullPath.ToString() +"已经被删除.\t";
} private void button1_Click(object sender, System.EventArgs e)
{
txt1.Text="";
} private void button2_Click(object sender, System.EventArgs e)
{
folderBrowserDialog1.ShowDialog();
txtpath.Text=folderBrowserDialog1.SelectedPath;
fileSystemWatcher1.Path = folderBrowserDialog1.SelectedPath;
}
}
}
想确定某个文件当前是否正在被写入应该是不可能的,但可以通过打开这个文件来判断此文件是否被其它程序占用。