C#怎么判断一个文件的状态(正在被读,正在

如题所述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
public static extern IntPtr _lopen(string lpPathName, int iReadWrite);

[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);
public const int OF_READWRITE = 2;
public const int OF_SHARE_DENY_NONE = 0x40;
public readonly IntPtr HFILE_ERROR = new IntPtr(-1);

public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{

string vFileName = @"C:\Users\E1\Desktop\CFP2\price.xlsx";
if (!File.Exists(vFileName))
{
MessageBox.Show("文件都不存在,你就不要拿来耍了");
return;
}
IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);
if (vHandle == HFILE_ERROR)
{
MessageBox.Show("文件被占用!");
return;
}
CloseHandle(vHandle);
MessageBox.Show("没有被占用!");

}
}
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答