string path = Server.MapPath(" PDFs"); bool tfOpenTemp= IsFileInUse(path + " /Doc1.pdf "); if (!tfOpenTemp) { } else { Alert("打印模板已调用,请关闭后进行此操作!"); }
#region 提示信息 //提示信息 using System.Web.UI; public void Alert(string str_Message) { ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript; scriptManager.RegisterStartupScript(typeof(string), "", "alert('" + str_Message + "');", true); } public void Alert(string str_Message, string redirect) { ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript; scriptManager.RegisterStartupScript(typeof(string), "", "alert('" + str_Message + "');self.location='" + redirect + "'", true); } #endregion
#region 判断文件是否已使用 public static bool IsFileInUse(string fileName) { bool inUse = true; if (File.Exists(fileName)) { FileStream fs = null; try { fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None); inUse = false; } catch (Exception e) { Console.WriteLine(e.Message.ToString()); } finally { if (fs != null) { fs.Close(); } } return inUse; //true表示正在使用,false没有使用 } else { return false; //文件不存在则一定没有被使用 } } #endregion