弱弱的问一下,如何判断文件正在被使用。 弱弱的问一下,如何判断文件正在被使用。写文件的时候,异常。描述 该文件正由另一个进程使用 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 什么类型的文件?文本的话可以这样试试System.IO.FileStream file = new FileStream( "C:\\file.txt ", FileMode.Open, FileAccess.Read, FileShare.None); 基他类型的,你这里自己找找吧http://www.baidu.com/s?bs=%C8%E7%BA%CE%C5%D0%B6%CF%CE%C4%BC%FE%D5%FD%D4%DA%B1%BB%CA%B9%D3%C3&f=8&wd=%C8%E7%BA%CE%C5%D0%B6%CF%CE%C4%BC%FE%D5%FD%D4%DA%B1%BB%CA%B9%D3%C3+asp.net try{ //写文件}catch(Exception ex){ //文件正在被使用 ex.message}效率比较低,或许有用。 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 5 public class FileUseTest 6 { 7 public static void Main() 8 { 9 FileUseTest fut = new FileUseTest();10 if(fut.IsUsed("c:\\1.doc"))11 {12 Console.WriteLine("文件已经被打开!");13 }14 15 Console.Read();16 17 }18 19 public bool IsUsed(String fileName)20 {21 bool result = false;22 23 try24 {25 FileStream fs = File.OpenWrite(fileName);26 fs.Close();27 }28 catch29 {30 result = true;31 }32 33 return result;34 }35 } 我不想用try catch 判断 try{File.Move}catch{}修改文件名[DllImport("kernel32.dll")]public static extern IntPtr _lopen(string lpPathName, int iReadWrite); [DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);private IntPtr xHandle;xHandle = FindWindow(null, pubV.window_Name);//如果xHandle不是0的话,就表示文在使用中 [DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);private IntPtr xHandle;xHandle = FindWindow(null, "window_Name");//如果xHandle不是0的话,就表示文在使用中上面pubV.window_Name是我自己定义的,你改成自己的文件名称, [DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);private IntPtr xHandle;xHandle = FindWindow(null, pubV.window_Name);//如果xHandle不是0的话,就表示文在使用中 window_Name 你有没有换成你那个文件的名字?记住不是以你文件命名的名字,而是文件打开后,任务管理器中或者标题栏上的那个,要一字不落 对不起,我的系统是英文的, 比如你在桌面新建一个一个EXCEL表格,新建好了叫 “New Microsoft Excel Worksheet”,打开后标题栏是“Microsoft Excel - New Microsoft Excel Worksheet”,那你要以后者为window_Name bool IsFileInUse(string fileName) { bool inUse = true; if (File.Exists(fileName)) { FileStream fs = null; try { fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None); inUse = false; } catch { // exception.... } finally { if (fs != null) fs.Close(); } return inUse;//by yl true表示正在使用,false没有使用 } else { return false;//文件不存在,肯定没有被使用 } } windows from中的listView如何通过item项的值选中该项 为什么if语句里面都用!操作符 GDI+绘图!获取一个不规则图形的边框的点的集合。 视频监控的问题 连接Access时显示:没有可用的错误消息 结果代码:E_FAIL(0x80004005) 求解:如何用一个字符串实例化另一个对象 请教:如何将一个C#文件.cs制成dll文件? 图像处理指针调用怎么出现问题? 根据Json数据,构建TreeView 写一个函数GetField(DataSet ds),在GetField中怎么样获取ds中的所有字段名? MediaPlayer全屏问题 C# 高效素数算法
{
//写文件
}
catch(Exception ex)
{
//文件正在被使用 ex.message}效率比较低,或许有用。
2 using System.Collections.Generic;
3 using System.IO;
4
5 public class FileUseTest
6 {
7 public static void Main()
8 {
9 FileUseTest fut = new FileUseTest();
10 if(fut.IsUsed("c:\\1.doc"))
11 {
12 Console.WriteLine("文件已经被打开!");
13 }
14
15 Console.Read();
16
17 }
18
19 public bool IsUsed(String fileName)
20 {
21 bool result = false;
22
23 try
24 {
25 FileStream fs = File.OpenWrite(fileName);
26 fs.Close();
27 }
28 catch
29 {
30 result = true;
31 }
32
33 return result;
34 }
35 }
{File.Move
}catch{}
修改文件名[DllImport("kernel32.dll")]
public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);private IntPtr xHandle;xHandle = FindWindow(null, pubV.window_Name);//如果xHandle不是0的话,就表示文在使用中
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);private IntPtr xHandle;xHandle = FindWindow(null, "window_Name");//如果xHandle不是0的话,就表示文在使用中上面pubV.window_Name是我自己定义的,你改成自己的文件名称,
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);private IntPtr xHandle;xHandle = FindWindow(null, pubV.window_Name);//如果xHandle不是0的话,就表示文在使用中
window_Name 你有没有换成你那个文件的名字?记住不是以你文件命名的名字,而是文件打开后,任务管理器中或者标题栏上的那个,要一字不落
bool IsFileInUse(string fileName) { bool inUse = true; if (File.Exists(fileName)) { FileStream fs = null; try { fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None); inUse = false; } catch { // exception.... } finally { if (fs != null) fs.Close(); } return inUse;//by yl true表示正在使用,false没有使用 } else { return false;//文件不存在,肯定没有被使用 } }