,too... I want to study
解决方案 »
- -----C# 导出excel 加超链接 怎么实现啊---------
- 求正则表达式
- 怎么将数据库中的2进制图片读入到byte[]里
- 求C#以不同算法写的迷宫算法
- 基于Windows操作系统小型局域网络入网自动化如何实现?
- 哪位高手有过开发支付网关的经验?
- using Graph = Microsoft.Office.Interop.Graph; 引用出错,请各位帮帮忙,先谢谢了
- 如何编辑listview中选中的项?
- 请帮忙:WinForm中两个DataGrid建立表间关系,我的程序只显示两张表,没有显示表间关系,请帮忙修改。谢谢!
- 无法接收谷歌输入法的中文
- 有关字符编码的小问题.
- 求一延时3分钟并和进程条结合起来的示例程序
None 谢绝共享当前文件。文件关闭前,打开该文件的任何请求(由此进程或另一进程发出的请求)都将失败。FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.None);
然后定时打开文件如果失败说明还没有关闭。
using System.IO;public class OpenTest
{
public static void Main()
{
// Open an existing file, or create a new one.
FileInfo fi = new FileInfo("temp.txt"); // Open the file just specified such that no one else can use it.
FileStream fs = fi.Open( FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None ); // Create another reference to the same file.
FileInfo nextfi = new FileInfo("temp.txt"); try
{
// Try opening the same file, which was locked by the previous process.
nextfi.Open( FileMode.OpenOrCreate, FileAccess.Read ); Console.WriteLine("The file was not locked, and was opened by a second process.");
}
catch (IOException)
{
Console.WriteLine("The file could not be opened because it was locked by another process.");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
} // Close the file so it can be deleted.
fs.Close();
}
}
如果采用隔段时间测试的方法,效率很低啊
有没有其他的方法啊
{ public static void Main()
{ string[] args = System.Environment.GetCommandLineArgs();
// If a directory is not specified, exit program.
if(args.Length != 2)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe (directory)");
return;
} // Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt"; // Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed); // Begin watching.
watcher.EnableRaisingEvents = true; // Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
} // Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
} private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}