zoukankan      html  css  js  c++  java
  • c# 判断文件是否已使用

                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
  • 相关阅读:
    OCP-1Z0-053-200题-91题-667
    OCP-1Z0-053-200题-92题-668
    OCP-1Z0-053-200题-93题-669
    OCP-1Z0-053-200题-94题-670
    OCP-1Z0-053-200题-96题-671
    OCP-1Z0-053-200题-97题-227
    OCP-1Z0-053-200题-98题-242
    OCP-1Z0-053-200题-99题-10
    在Visual Studio 2010中配置VC++目录
    OCP-1Z0-053-200题-100题-69
  • 原文地址:https://www.cnblogs.com/yimeishui/p/6201501.html
Copyright © 2011-2022 走看看