コード例 #1
0
        public void Update(DataTable files)
        {
            if (files == null)
            {
                throw new Exception("No se actualizaron archivos porque no se proporcionaron");
            }
            DateTime mmfirst         = System.DateTime.Now;
            string   excludefilename = "";

#if PocketPC
#else
            if (ExcludeExecutingAssembly)
            {
                excludefilename = System.Reflection.Assembly.GetExecutingAssembly().Location;
                excludefilename = Path.GetFileName(excludefilename);
                excludefilename = excludefilename.ToUpper();
            }
#endif
            const int BUFSIZE = 8192;
            byte[]    buf     = new byte[BUFSIZE];

            string backpath = "";
            // Create temp dir
            if (PerformBackup)
            {
                backpath = FFilePath + "CC" + System.DateTime.Now.ToString("ddMMyyyyHH_mm_ss");
                Directory.CreateDirectory(backpath);
            }
            int countfile = 1;

            // Check if all the files are ready to upgrade
            foreach (DataRow frow in files.Rows)
            {
                DateTime datemodified = (DateTime)frow["MODIFIED"];
                string   npath        = Path.Combine(FFilePath, frow["PATH"].ToString());
                Directory.CreateDirectory(npath);
                string   filename = npath + Path.DirectorySeparatorChar + frow["FILE"].ToString();
                FileInfo nfinfo   = new FileInfo(filename);
                bool     docancel = false;
                if (OnProgress != null)
                {
                    OnProgress(filename, countfile, files.Rows.Count, 0, ((byte[])frow["STREAM"]).Length, ref docancel);
                }
                bool requireupgrade = true;
                if (nfinfo.Exists)
                {
#if PocketPC
                    /*DateTime nlastwritetime;
                     * if (OnGetLastWriteTime != null)
                     *  nlastwritetime = OnGetLastWriteTime(filename);
                     * else
                     *  nlastwritetime = nfinfo.LastWriteTime;
                     * if (nlastwritetime >= datemodified)
                     * requireupgrade = false;*/
                    requireupgrade = true;
#else
//              if (nfinfo.LastWriteTimeUtc >= datemodified)
                    if (nfinfo.LastWriteTime >= datemodified)
                    {
                        requireupgrade = false;
                    }
#endif
                }
                else
                {
                    requireupgrade = false;
                }

                if ((excludefilename.Length > 0) && requireupgrade)
                {
                    if (frow["FILE"].ToString().ToUpper() == excludefilename)
                    {
                        requireupgrade = false;
                    }
                }
                if (requireupgrade)
                {
                    if (StreamUtil.FileInUse(filename))
                    {
                        throw new Exception("File in use: " + filename);
                    }
                }
            }
            foreach (DataRow xrow in files.Rows)
            {
                // Only update if version newer
                DateTime datemodified = (DateTime)xrow["MODIFIED"];
                string   npath        = Path.Combine(FFilePath, xrow["PATH"].ToString());
                Directory.CreateDirectory(npath);
                string   filename = npath + Path.DirectorySeparatorChar + xrow["FILE"].ToString();
                FileInfo nfinfo   = new FileInfo(filename);
                bool     docancel = false;
                if (OnProgress != null)
                {
                    OnProgress(filename, countfile, files.Rows.Count, 0, ((byte[])xrow["STREAM"]).Length, ref docancel);
                }
                bool doupdate = true;
                if (nfinfo.Exists)
                {
#if PocketPC
/*            DateTime nlastwritetime;
 *          if (OnGetLastWriteTime != null)
 *              nlastwritetime = OnGetLastWriteTime(filename);
 *          else
 *              nlastwritetime = nfinfo.LastWriteTime;
 *          if (nlastwritetime >= datemodified)
 *              doupdate = false;*/
                    doupdate = true;
#else
//          if (nfinfo.LastWriteTimeUtc >= datemodified)
                    if (nfinfo.LastWriteTime >= datemodified)
                    {
                        doupdate = false;
                    }
#endif
                }
                if (excludefilename.Length > 0)
                {
                    if (xrow["FILE"].ToString().ToUpper() == excludefilename)
                    {
                        doupdate = false;
                    }
                }
                if (doupdate)
                {
                    // Backup file
                    //System.Threading.Thread.Sleep(1000);

                    if (PerformBackup)
                    {
                        string nbackpath = Path.Combine(backpath, xrow["PATH"].ToString());
                        Directory.CreateDirectory(nbackpath);
                        if (File.Exists(filename))
                        {
                            File.Move(filename, nbackpath + Path.DirectorySeparatorChar + xrow["FILE"].ToString());
                        }
                    }
                    DateTime mmlast;
                    TimeSpan difmilis;
                    byte[]   original = (byte[])xrow["STREAM"];
                    using (FileStream fstream = new FileStream(filename, FileMode.Create, FileAccess.Write))
                    {
                        int index        = 0;
                        int totalwritten = 0;
                        do
                        {
                            int towrite = BUFSIZE;
                            if ((original.Length - index) < BUFSIZE)
                            {
                                towrite = (original.Length - index);
                            }
                            fstream.Write(original, index, towrite);
                            totalwritten = totalwritten + towrite;
                            index        = index + towrite;
                            if (OnProgress != null)
                            {
                                mmlast   = System.DateTime.Now;
                                difmilis = mmlast - mmfirst;
                                if (difmilis.TotalMilliseconds > 500)
                                {
                                    OnProgress(filename, countfile, files.Rows.Count, totalwritten, original.Length, ref docancel);
                                    mmfirst = System.DateTime.Now;
                                }
                            }
                        } while (index < original.Length);
                    }
                    OnProgress(filename, countfile, files.Rows.Count, original.Length, original.Length, ref docancel);

                    nfinfo = new FileInfo(filename);
#if PocketPC
                    if (OnSetLastWriteTime != null)
                    {
                        OnSetLastWriteTime(filename, datemodified);
                    }
                    else
                    {
                        throw new Exception("OnSetLastWriteTime event must be provided");
                    }
                    //nfinfo.LastWriteTime = datemodified;
#else
//          nfinfo.LastWriteTimeUtc = datemodified;
                    nfinfo.LastWriteTime = datemodified;
#endif
                    countfile++;
                }
            }
        }