Excel导入时 判断是否被占用
来源:互联网 发布:趣味百科软件 编辑:程序博客网 时间:2024/05/12 02:27
Excel导入时 判断是否被占用,三种方法:
1:Win7可以,WIN10不可以
try { //原理,如果文件可以被移动,说明未被占用 string strPath = "C:\\123OK.Excel"; string strPath2 = "C:\\123OK22.Excel"; File.Move(strPath, strPath2); File.Move(strPath2, strPath); } catch { MessageBox.Show("文件被占用!"); return; }
2:文件流
try { //原理,如果文件可写,说明未被占用 System.IO.FileStream stream = System.IO.File.OpenWrite("文件路径"); stream.Close(); } catch { MessageBox.Show("文件被占用!"); return; }
3:WIN32 API调用(强烈推荐)
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; } } }}
0 0
- Excel导入时 判断是否被占用
- 判断文件是否被占用
- 判断文件是否被占用
- Excel操作Bug引出的问题:如何判断文件是否被占用、打开
- 使用反射,在Excel导入时判断是否有空行
- 判断文件是否已被占用
- 判断文件是否已被占用
- 怎么判断文件是否被占用
- 如何判断一个端口是否被占用
- 使用Socket判断端口是否被占用
- C# 判断文件是否被占用
- 判断文件是否被占用的方法
- c# socket 判断端口是否被占用
- Linux C++判断端口是否被占用
- C# 判断文件夹状态,是否被占用。。。。。
- delphi 判断端口(Port)是否被占用
- Ajax判断用户名是否被占用
- 原生方式判断用户名是否被占用
- Android引导页面的实现
- Android中用广播从Service中向Activity发送信息
- 路由器学习
- 3dmax布料动画导入到Unity流程
- UIViewController的生命周期及iOS程序执行顺序
- Excel导入时 判断是否被占用
- xslt中让两个fo:block处于同一行,展示不同的样式
- Pandas 文本数据方法 cat()
- MATLAB画阶梯图
- SAP报表开发
- android插件的SourceSets
- 入门级Hadoop集群搭建详细教程(十一):Hadoop集群的搭建
- Python输出字符串问题
- 关于JS正则表达式中g模式下test出现true/false交替的个人见解