zoukankan      html  css  js  c++  java
  • C# 检查文件是否被进程占用

            [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);
            private void CheckFile(string filepath)
            {
                string vFileName = filepath;
                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("没有被占用!");
            }

  • 相关阅读:
    Python--安装 pip 和 scapy
    windows设置代理
    麦子的《我奋斗了18年才和你坐在一起喝咖啡》
    Tar命令
    test
    markdown语法
    js apply call
    windows文件大小和占用空间为何不一样
    prolog笔记
    html文字超出显示省略号
  • 原文地址:https://www.cnblogs.com/ybb521/p/3367658.html
Copyright © 2011-2022 走看看