没空啰嗦,直接上代码!

class FileOperate

{
        //for security , not display the account in the app.config
        private string RemoteServer = ConfigurationSettings.AppSettings["RemoteServer"];
        private string sharefolder = @"\\" + ConfigurationSettings.AppSettings["RemoteServer"];
        private string localfolder = ConfigurationSettings.AppSettings["RemoteLocalDisk"];
        [DllImport("kernel32.dll")]
        private static extern IntPtr _lopen(string lpPathName, int iReadWrite);
        [DllImport("kernel32.dll")]
        private static extern bool CloseHandle(IntPtr hObject);
        private const int OF_READWRITE = 2;
        private const int OF_SHARE_DENY_NONE = 0x40;
        private readonly IntPtr HFILE_ERROR = new IntPtr(-1);

private string CloseFileHandler(string trgFile)
        {
            string strReturn = "";
            try
            {
                trgFile = trgFile.ToLower();
                string local_file_name = trgFile.Replace(sharefolder, localfolder);

System.Diagnostics.Process p = new System.Diagnostics.Process();

p.StartInfo.FileName = "cmd.exe";           //设定程序名

string  strLoginName="XXXXX"; //登录名

string strPWD = Encryption.DESDecrypt("XXXXXXX"); //登陆密码

string cmd_args = " /c openfiles /disconnect /op \"" + local_file_name + "\" /ID * /s " + RemoteServer + " /u "+strLoginName+" /p " + strPWD;
                p.StartInfo.Arguments = cmd_args;           //设定程式执行参数
                p.StartInfo.UseShellExecute = false;        //关闭Shell的使用
                p.StartInfo.RedirectStandardInput = true;   //重定向标准输入
                p.StartInfo.RedirectStandardOutput = true;  //重定向标准输出
                p.StartInfo.RedirectStandardError = true;   //重定向错误输出
                p.StartInfo.CreateNoWindow = true;          //设置不显示窗口
                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

p.Start();                                  //启动程序
                p.WaitForExit();
            }
            catch(Exception ex)
            {
                strReturn = ex.Message;
            }

return strReturn;

}

public string TryReleaseFileHandler(string strSourchPathFile)
        {
            string strReturn = "";
            IntPtr vHandle = _lopen(strSourchPathFile, OF_READWRITE | OF_SHARE_DENY_NONE);

try
            {
                if (vHandle == HFILE_ERROR)
                {
                   strReturn = CloseFileHandler(strSourchPathFile);
                }
            }
            catch (Exception ex)
            {
                strReturn = ex.Message;
            }
            finally
            {
                CloseHandle(vHandle);
            }

return strReturn;

}
    }

处理文件时遇到:The process cannot access the file because it is being used by another process.相关推荐

  1. smbException:The process cannot access the file because it is being used by another process的错误原因

    smbException:The process cannot access the file because it is being used by another process 问题我搜了百度上 ...

  2. golang错误:The process cannot access the file because it is being used by another process

    情况一,创建文件后再删除: func main() {NewConfigIni()DeleteConfigTemp() }func DeleteConfigTemp() {path := " ...

  3. the process cannot access the file because it is being used by another process

    当在IIS中修改绑定的端口号后启动时遇到如下错误,表明你的端口号已经被占用了 使用netstat -ano这个指令查看本地端口占用情况,换一个没有被占用的端口即可.  版权声明:本文为博主原创文章 ...

  4. goland编译时提示\go-build\exe\a.out.exe: The process cannot access the file because it is being used

    goland中build一直提示 open C:\Users\ \AppData\Local\Temp\go-build2905132708\b001\exe\a.out.exe: The proce ...

  5. 使用Androidkiller编译APK文件时出现libpng error: Not a PNG file的错误

    使用Androidkiller或APKIDE编译APK文件时出现提示: >W: libpng error: Not a PNG file >W: ERROR: Failure proces ...

  6. linux 下scp传文件时,报错 not a regular file

    linux 下scp传文件时错误 scp: /usr/tools: not a regular file 不能成功传送 解决方案 1:传输的文件没可执行权限 chmod 777 2:  使用scp时加 ...

  7. <VS Code>关于vscode编译c++文件时出现【error:no such file or directory】的错误

    前言 人生如逆旅,我亦是行人. 今天在用 vscode 编译写好的C++文件的时候,突然出现了以下的错误: [error:no such file or directory] 其实解决之后发现也不是什 ...

  8. CLin 和 IDEA创建文件时自动添加作者和时间信息 + 多个.cpp文件在CLion下运行问题...

    CLin 和 IDEA创建文件时自动添加作者和时间信息 file -> setting -> Editor -> File and Code Templates -> 点击In ...

  9. python创建文件对象的函数_Python学习笔记之—— File(文件) 对象常用函数

    file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 1.file.close() close() 方法用于关闭一个已打开的文件.关闭后的文件不能再进行读写操作, 否则会触 ...

最新文章

  1. 企业信息化投入中咨询服务_企业信息化咨询中的问题与对策研究
  2. 几个 PHP 的“魔术常量”
  3. 【Android Developers Training】 6. 配置Action Bar
  4. 一道丧心病狂的java面试题
  5. 在实践中使用延迟队列
  6. JBoss5开发web service常见问题
  7. 1.1 HTML5简介
  8. 微课|《Python编程基础与案例集锦(中学版)》第2章(1)
  9. java学习之类型转换与越界
  10. [Codeforces235D]Graph Game——概率与期望+基环树+容斥
  11. windows下 Mysql 错误1067 Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
  12. 有关C++的标准模板库(STL)的一些个人易错点
  13. 谈谈坚持写博客的感悟
  14. 红队免杀培训第二章-使用系统调用http 协议下载恶意载荷
  15. java calendar时间计算_Java Calendar计算所在时区的时间偏移量
  16. 通过you-get在哔哩哔哩下载视频报错处理
  17. linux服务篇-Nagios监控
  18. NFC开发 —————实用工具以及开发文档(四)
  19. 软件测试可以做到多大岁数?
  20. MySQL——O4. 表结构设计和数据类型优化

热门文章

  1. java怎么循环_java怎么实现循环
  2. After Effects (AE)2021软件下载及安装教程。
  3. mac远程桌面连接windows_mac连接windows远程桌面的方法
  4. spring cloud alibaba - sentinelOpenFeign使用
  5. 超快速实现 svg 转 png,jpg等格式
  6. 为什么要使用线程池?线程池有什么作用?
  7. 2022年全球市场穿梭式货架总体规模、主要生产商、主要地区、产品和应用细分研究报告
  8. Ewebeditor最新漏洞及漏洞大全
  9. Qos-限速实例 根据不通的IP地址来限速
  10. 第5-4课:欧拉图与弗罗莱(Fleury)算法