今天很无语,要解决一个问题,Silverlight中无法对用户打开的文件进行判断,是否打开,沙箱模式运作,出于安全性考虑,是无法获得
本地文件的绝对路径,无法调用API来判断实现这功能,最后还是用Trycatch来解决。
C#能够调用的API都能从以下网站查到:
API查询网站:http://www.webtropy.com/articles/Win32-API-DllImport-art9.asp
还是写下C# 调用系统API来实现文件判断效果:
1.命名空间:using System.Runtime.InteropServices
2. API函数 _lopen(string pathfile,int ireadwriter)
1 /// <summary>
2 /// Description of UseAPI.
3 /// </summary>
4 public class UseAPI
5 {
6
7 public UseAPI()
8 {
9 }
10
11 private const int OF_READWRITE = 2;
12 private const int OF_SHARE_DENY_NONE = 0x40;
13 private static readonly IntPtr HFILE_ERROR = new IntPtr(-1);
14
15
16 /// <summary>
17 /// 判断文件是否打开
18 /// </summary>
19 /// <param name="lpPathName">文件名称</param>
20 /// <param name="iReadWrite"></param>
21 /// <returns></returns>
22 [DllImport("kernel32.dll")]
23 private static extern IntPtr _lopen(string lpPathName, int iReadWrite);
24
25 /// <summary>
26 /// 关闭文件句柄
27 /// </summary>
28 /// <param name="hObject"></param>
29 /// <returns></returns>
30 [DllImport("kernel32.dll")]
31 private static extern bool CloseHandle(IntPtr hObject);
32
33 /// <summary>
34 /// 文件名称
35 /// </summary>
36 /// <param name="filename"></param>
37 /// <returns></returns>
38 public static string IsOpen(string filename)
39 {
40 IntPtr vHandle = _lopen(filename, OF_READWRITE | OF_SHARE_DENY_NONE);
41 if (vHandle == HFILE_ERROR)
42 {
43 CloseHandle(vHandle);
44 return "文件被占用!";
45 }
46 else
47 return "没有被占用!";
48
49 //CloseHandle(vHandle);
50 }
51 }