码迷,mamicode.com
首页 > Windows程序 > 详细

C#解除文件锁定

时间:2018-05-12 22:27:12      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:handler   wildcards   窗口   NPU   int   进程   tin   void   工具   

public static void StreamsFile(string fi)
        {
                try
                {
                    var p = new Process
                    {
                        StartInfo =
                        {
                            FileName = Environment.CurrentDirectory + "\\streams64.exe",
                            UseShellExecute = false,
                            Arguments = " -d \""+fi+"\""
                        }
                    };

                    //执行参数
                    p.StartInfo.UseShellExecute = false;  ////不使用系统外壳程序启动进程
                    p.StartInfo.CreateNoWindow = true;  //不显示dos程序窗口
                    p.StartInfo.RedirectStandardInput = true;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中
                    p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
                    p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
                    p.StartInfo.UseShellExecute = false;
                    p.Start();
                    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    p.BeginErrorReadLine();//开始异步读取
                    p.WaitForExit();//阻塞等待进程结束
                    p.Close();//关闭进程
                    p.Dispose();//释放资源

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }

            public static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
            {
                Console.WriteLine(e.Data);
            }
            public static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
            {
                Console.WriteLine(e.Data);
            }

刚刚碰到过这个问题,要使用微软的一个工具,可以在C#代码中通过Process的方式,命令行执行这个工具,就可以解锁了.
具体看:

Using Streams

Usage: streams [-s] [-d] <file or directory>
-s Recurse subdirectories.
-d Delete streams. 
Streams takes wildcards e.g. ‘streams *.txt‘.

https://technet.microsoft.com/en-us/Sysinternals/bb897440.aspx

 

https://download.sysinternals.com/files/Streams.zip

 

C#解除文件锁定

标签:handler   wildcards   窗口   NPU   int   进程   tin   void   工具   

原文地址:https://www.cnblogs.com/littlehb/p/9030019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!