gpt4 book ai didi

c# - 有没有办法检查文件是否正在使用

转载 作者:行者123 更新时间:2023-12-03 00:35:27 25 4
gpt4 key购买 nike

使用C#,可以检查文件是否在使用中。更具体地说,我需要一种方法来监视.wav文件的目录并查看已加载的声音文件。
注意。可以由不同的应用程序加载。
我看过Core Audio ..但我似乎找不到答案。
我也尝试编码,如果文件被锁定,但这也没有提供解决方案。因此,如果您知道确定当前正在播放哪个声音文件(.wav)的方法,请发表评论。

最佳答案

protected virtual bool IsFileLocked(FileInfo file)
{
FileStream stream = null;

try
{
stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null)
stream.Close();
}

//file is not locked
return false;
}

这是引用链接: File is in Use

希望能帮助到你。

关于c# - 有没有办法检查文件是否正在使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41936628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com