处理文件时遇到:The process cannot access the file because it is being used by another process.
来源:互联网 发布:数控车编程培训 编辑:程序博客网 时间:2024/05/17 02:37
没空啰嗦,直接上代码!
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;
}
}
0 0
- 处理文件时遇到:The process cannot access the file because it is being used by another process.
- 拷贝文件时出现The process cannot access the file 'xxx' because it is being used by another process.错误
- The process cannot access the file XXX because it is being used by another process
- IIS:The process cannot access the file because it is being used by another process.
- FileStream:The process cannot access the file because it is being used by another process
- The process cannot access the file because it is being used by another process.
- Solution: The process cannot access the file [filename] because it is being used by another process.
- the process cannot access the file because it is being used by another process
- C#使用Create创建文件后,报The Process cannot access the file because it is being used by another process的异常
- C#使用Create创建文件后,报The Process cannot access the file because it is being used by another process的异常
- IIS ERROR about SP:The process can't access the file because it is being used by another process
- 【IIS】the process can not access the file because the file is being used by another process
- 文件流操作,报“because it is being used by another process. ”错误解决
- Visual studio bin/Debug/… because it is being used by another process
- Error 3724:Cannot drop the table 'object_name' because it is being used for replication.
- Error 3724:Cannot drop the table 'object_name' because it is being used for replication.
- Cannot drop the database ‘XXX’ because it is being used for replication.
- Cannot drop the database XXX because it is being used for replication
- GCC内嵌汇编
- 赚钱的8大定律
- H5B之3客户端Web Hello World教程
- [BZOJ1024][SCOI2009][DFS]生日快乐
- window phone7中自定义listbox或scrollviewer滚动条样式
- 处理文件时遇到:The process cannot access the file because it is being used by another process.
- IPMI学习(supermicro)
- Ext.NET -- 后台动态添加控件
- 关于hashCode方法的作用
- 配置 yum 源的两种方法
- 黑马程序员学习笔记——泛型的反射
- 关于指针数组和数组指针的浅谈
- Struts
- [BZOJ3171][TJOI2013][最小费用最大流]循环格