C#解除文件锁定

本文介绍如何使用C#调用微软的Streams工具解锁文件。通过Process类执行Streams工具,实现对指定文件删除其属性的功能。文章提供了具体的C#代码示例,并说明了Streams工具的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/littlehb/p/9030019.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值