It ain't clean, but it works:
try
{
using (Stream stream = new FileStream("File.txt"))
{
}
}
catch
{
// Check if file is in use (or whatever else went wrong), and do any user communication that you need to
}
I don't particularly like using exceptions as conditions, but as far as I know this is about the easiest and best (and only reliable) way to do this.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…