国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 中Excel導入時判斷是否被占用三種方法

2019-10-29 21:11:18
字體:
來源:轉載
供稿:網友

C# 中Excel導入時 判斷是否被占用三種方法

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;    }   }  } } 

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五河县| 大城县| 萝北县| 彰武县| 乌海市| 柳林县| 华宁县| 佳木斯市| 蓬莱市| 满洲里市| 合江县| 都匀市| 临潭县| 股票| 焉耆| 湖口县| 蓝田县| 南江县| 怀集县| 阜宁县| 保康县| 万全县| 梓潼县| 旬邑县| 文安县| 股票| 孟村| 宁城县| 勐海县| 新邵县| 金川县| 含山县| 额尔古纳市| 江源县| 文水县| 奉新县| 铅山县| 洪泽县| 金堂县| 和平区| 苏尼特左旗|