try
{
//原理,如果文件可以被移动,说明未被占用
string strPath = "C:\\123OK.Excel";
string strPath2 = "C:\\123OK22.Excel";
File.Move(strPath, strPath2);
File.Move(strPath2, strPath);
}
catch
{
MessageBox.Show("文件被占用!");
return;
}
try
{
//原理,如果文件可写,说明未被占用
System.IO.FileStream stream = System.IO.File.OpenWrite("文件路径");
stream.Close();
}
catch
{
MessageBox.Show("文件被占用!");
return;
}
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 button1_Click(object sender, EventArgs e)
{
try
{
string vFileName = @"c:\123.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("没有被占用!");
}
catch (Exception ex)
{
throw ex;
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有