积极答复者
vb2008检测文件是否被打开的方法

问题
答案
-
你好,我使用FileStream的FileShare枚举设置,查看抛出异常的示例代码——
static void Main(string[] args)
{
using (FileStream fs = new FileStream("D:\\Try.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None)) //注意下划线的设置!
{
try
{
FileStream fs2 = new FileStream("D:\\Try.txt",FileMode.Open); //这里企图再次打开没有被关闭的文件……
}
catch (Exception)
{Console.WriteLine("不能打开文件,因为是独享的!");
}
}
}【vb代码】
Private Shared Sub Main(args As String()) Using fs As New FileStream("D:\Try.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None) '注意下划线的设置! Try '这里企图再次打开没有被关闭的文件…… Dim fs2 As New FileStream("D:\Try.txt", FileMode.Open) Catch generatedExceptionName As Exception Console.WriteLine("不能打开文件,因为是独享的!") End Try End Using End Sub
如果你有其它意见或私下交流,请点击此处
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处
- 已标记为答案 pucx 2011年5月13日 15:43
全部回复
-
要在驱动层做,VB做不了。
你到底想做什么?如果你要写入文件,唯一可靠的检测办法就是打开文件写入数据。
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
你好,我使用FileStream的FileShare枚举设置,查看抛出异常的示例代码——
static void Main(string[] args)
{
using (FileStream fs = new FileStream("D:\\Try.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None)) //注意下划线的设置!
{
try
{
FileStream fs2 = new FileStream("D:\\Try.txt",FileMode.Open); //这里企图再次打开没有被关闭的文件……
}
catch (Exception)
{Console.WriteLine("不能打开文件,因为是独享的!");
}
}
}【vb代码】
Private Shared Sub Main(args As String()) Using fs As New FileStream("D:\Try.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None) '注意下划线的设置! Try '这里企图再次打开没有被关闭的文件…… Dim fs2 As New FileStream("D:\Try.txt", FileMode.Open) Catch generatedExceptionName As Exception Console.WriteLine("不能打开文件,因为是独享的!") End Try End Using End Sub
如果你有其它意见或私下交流,请点击此处
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处
- 已标记为答案 pucx 2011年5月13日 15:43