Пример #1
0
        /// <summary>
        /// 获取指定文件大小
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public long GetFileSize(string filename)
        {
            FtpWebRequest reqFTP;
            long          fileSize = 0;

            try
            {
                reqFTP             = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename));
                reqFTP.Method      = WebRequestMethods.Ftp.GetFileSize;
                reqFTP.UseBinary   = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response  = (FtpWebResponse)reqFTP.GetResponse();
                Stream         ftpStream = response.GetResponseStream();
                fileSize = response.ContentLength;

                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                //Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFileSize Error --> " + ex.Message);
                LogClass.WriteLogFile(ex.ToString());
            }
            return(fileSize);
        }
Пример #2
0
        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="folderName"></param>
        public void RemoveDirectory(string folderName)
        {
            try
            {
                string        uri = ftpURI + folderName;
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.KeepAlive   = false;
                reqFTP.Method      = WebRequestMethods.Ftp.RemoveDirectory;

                string         result     = String.Empty;
                FtpWebResponse response   = (FtpWebResponse)reqFTP.GetResponse();
                long           size       = response.ContentLength;
                Stream         datastream = response.GetResponseStream();
                StreamReader   sr         = new StreamReader(datastream);
                result = sr.ReadToEnd();
                sr.Close();
                datastream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                LogClass.WriteLogFile(ex.ToString());
                //Insert_Standard_ErrorLog.Insert("FtpWeb", "Delete Error --> " + ex.Message + "  文件名:" + folderName);
            }
        }
Пример #3
0
        private VersionModel ReadConfig(string filepath)
        {
            VersionModel version  = new VersionModel();
            XmlDocument  xd       = new XmlDocument();
            string       fileName = filepath;

            try
            {
                xd.Load(fileName);

                version.LastTime  = xd.SelectSingleNode("//LastUpdateTime").InnerText;
                version.Version   = xd.SelectSingleNode("//Version").InnerText;
                version.PubNumber = xd.SelectSingleNode("//PubNumber").InnerText;
                XmlNodeList FileList = xd.SelectNodes("//UpdateFileList/File");
                foreach (XmlNode xn in FileList)
                {
                    string      ver       = xn.Attributes["Ver"].Value;
                    string      lasttime  = xn.Attributes["LastTime"].Value;
                    string      name      = xn.Attributes["Name"].Value;
                    string      pubnumber = xn.Attributes["PubNumber"].Value;
                    FileDetails file      = new FileDetails();
                    file.lastime          = lasttime;
                    file.version          = ver;
                    file.pubnumber        = pubnumber;
                    version.fileDic[name] = file;
                }
            }
            catch (Exception e)
            {
                LogClass.WriteLogFile("readconfig error:" + e.Message);
            }
            return(version);
        }
Пример #4
0
        /// <summary>
        /// 删除订单目录
        /// </summary>
        /// <param name="ftpServerIP">FTP 主机地址</param>
        /// <param name="folderToDelete">FTP 用户名</param>
        /// <param name="ftpUserID">FTP 用户名</param>
        /// <param name="ftpPassword">FTP 密码</param>
        public static void DeleteOrderDirectory(string ftpServerIP, string folderToDelete, string ftpUserID, string ftpPassword)
        {
            try
            {
                if (!string.IsNullOrEmpty(ftpServerIP) && !string.IsNullOrEmpty(folderToDelete) && !string.IsNullOrEmpty(ftpUserID) && !string.IsNullOrEmpty(ftpPassword))
                {
                    FTPHelper fw = new FTPHelper(ftpServerIP, folderToDelete, ftpUserID, ftpPassword, ftpPassive);
                    //进入订单目录
                    fw.GotoDirectory(folderToDelete, true);
                    //获取规格目录
                    string[] folders = fw.GetDirectoryList();
                    foreach (string folder in folders)
                    {
                        if (!string.IsNullOrEmpty(folder) || folder != "")
                        {
                            //进入订单目录
                            string subFolder = folderToDelete + "/" + folder;
                            fw.GotoDirectory(subFolder, true);
                            //获取文件列表
                            string[] files = fw.GetFileList("*.*");
                            if (files != null)
                            {
                                //删除文件
                                foreach (string file in files)
                                {
                                    fw.Delete(file);
                                }
                            }
                            //删除冲印规格文件夹
                            fw.GotoDirectory(folderToDelete, true);
                            fw.RemoveDirectory(folder);
                        }
                    }

                    //删除订单文件夹
                    string parentFolder = folderToDelete.Remove(folderToDelete.LastIndexOf('/'));
                    string orderFolder  = folderToDelete.Substring(folderToDelete.LastIndexOf('/') + 1);
                    fw.GotoDirectory(parentFolder, true);
                    fw.RemoveDirectory(orderFolder);
                }
                else
                {
                    throw new Exception("FTP 及路径不能为空!");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("删除订单时发生错误,错误信息为:" + ex.Message);
                LogClass.WriteLogFile(ex.ToString());
            }
        }
Пример #5
0
        /// <summary>
        /// 获取当前目录下文件列表(仅文件)
        /// </summary>
        /// <returns></returns>
        public string[] GetFileList(string mask)
        {
            string[]      downloadFiles;
            StringBuilder result = new StringBuilder();
            FtpWebRequest reqFTP;

            try
            {
                reqFTP             = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
                reqFTP.UseBinary   = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.Method      = WebRequestMethods.Ftp.ListDirectory;
                WebResponse  response = reqFTP.GetResponse();
                StreamReader reader   = new StreamReader(response.GetResponseStream(), Encoding.Default);

                string line = reader.ReadLine();
                while (line != null)
                {
                    if (mask.Trim() != string.Empty && mask.Trim() != "*.*")
                    {
                        string mask_ = mask.Substring(0, mask.IndexOf("*"));
                        if (line.Substring(0, mask_.Length) == mask_)
                        {
                            result.Append(line);
                            result.Append("\n");
                        }
                    }
                    else
                    {
                        result.Append(line);
                        result.Append("\n");
                    }
                    line = reader.ReadLine();
                }
                result.Remove(result.ToString().LastIndexOf('\n'), 1);
                reader.Close();
                response.Close();
                return(result.ToString().Split('\n'));
            }
            catch (Exception ex)
            {
                downloadFiles = null;
                if (ex.Message.Trim() != "远程服务器返回错误: (550) 文件不可用(例如,未找到文件,无法访问文件)。")
                {
                    //Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFileList Error --> " + ex.Message.ToString());
                }
                LogClass.WriteLogFile(ex.ToString());
                return(downloadFiles);
            }
        }
Пример #6
0
        /// <summary>
        /// 通过FTP下载文件到本地
        /// </summary>
        /// <param name="filePath">本地文件存储路径</param>
        /// <param name="fileName">本机文件名</param>
        public bool Download(string filePath, string fileName)
        {
            bool          result = false;
            FtpWebRequest reqFTP;

            try
            {
                LogClass.WriteLogFile("Download:" + filePath + "," + fileName);
                LogClass.WriteLogFile("Download:" + ftpURI + fileName);
                FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create);

                reqFTP             = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
                reqFTP.Method      = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary   = ftpBinary;
                reqFTP.UsePassive  = ftpPassive;
                reqFTP.KeepAlive   = ftpKeepAlive;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response   = (FtpWebResponse)reqFTP.GetResponse();
                Stream         ftpStream  = response.GetResponseStream();
                long           cl         = response.ContentLength;
                int            bufferSize = 2048;
                int            readCount;
                byte[]         buffer = new byte[bufferSize];

                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

                ftpStream.Close();
                outputStream.Close();
                response.Close();
                result = true;
            }
            catch (Exception ex)
            {
                //Insert_Standard_ErrorLog.Insert("FtpWeb", "Download Error --> " + ex.Message);
                result = false;
                LogClass.WriteLogFile(ex.ToString());
            }

            return(result);
        }
Пример #7
0
        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="filename">文件路径(包含文件名称)</param>
        public string Upload(string filename)
        {
            string        result  = string.Empty;
            FileInfo      fileInf = new FileInfo(filename);
            string        uri     = ftpURI + fileInf.Name;
            FtpWebRequest reqFTP;

            MakeDir("");
            reqFTP               = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            reqFTP.Credentials   = new NetworkCredential(ftpUserID, ftpPassword);
            reqFTP.UsePassive    = ftpPassive;
            reqFTP.KeepAlive     = ftpKeepAlive;
            reqFTP.Method        = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary     = ftpBinary;
            reqFTP.ContentLength = fileInf.Length;
            int buffLength = 2048;

            byte[]     buff = new byte[buffLength];
            int        contentLen;
            FileStream fs = fileInf.OpenRead();

            //FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
            try
            {
                Stream strm = reqFTP.GetRequestStream();
                contentLen = fs.Read(buff, 0, buffLength);
                while (contentLen != 0)
                {
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }
                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                result = ex.ToString();
                LogClass.WriteLogFile(ex.ToString());
                //Insert_Standard_ErrorLog.Insert("FtpWeb", "Upload Error --> " + ex.Message);
            }
            return(result);
        }
Пример #8
0
        /// <summary>
        /// 获取当前目录下明细(包含文件和文件夹)
        /// </summary>
        /// <returns></returns>
        public string[] GetFilesDetailList()
        {
            string[] downloadFiles;
            try
            {
                StringBuilder result = new StringBuilder();
                FtpWebRequest ftp;
                ftp             = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
                ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                ftp.Method      = WebRequestMethods.Ftp.ListDirectoryDetails;
                WebResponse  response = ftp.GetResponse();
                StreamReader reader   = new StreamReader(response.GetResponseStream(), Encoding.Default);

                //while (reader.Read() > 0)
                //{

                //}
                string line = reader.ReadLine();
                //line = reader.ReadLine();
                //line = reader.ReadLine();

                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    line = reader.ReadLine();
                }
                result.Remove(result.ToString().LastIndexOf("\n"), 1);
                reader.Close();
                response.Close();
                return(result.ToString().Split('\n'));
            }
            catch (Exception ex)
            {
                downloadFiles = null;
                //Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFilesDetailList Error --> " + ex.Message);
                LogClass.WriteLogFile(ex.ToString());
                return(downloadFiles);
            }
        }
Пример #9
0
        /// <summary>
        /// 改名
        /// </summary>
        /// <param name="currentFilename"></param>
        /// <param name="newFilename"></param>
        public void ReName(string currentFilename, string newFilename)
        {
            FtpWebRequest reqFTP;

            try
            {
                reqFTP             = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + currentFilename));
                reqFTP.Method      = WebRequestMethods.Ftp.Rename;
                reqFTP.RenameTo    = newFilename;
                reqFTP.UseBinary   = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response  = (FtpWebResponse)reqFTP.GetResponse();
                Stream         ftpStream = response.GetResponseStream();

                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                //Insert_Standard_ErrorLog.Insert("FtpWeb", "ReName Error --> " + ex.Message);
                LogClass.WriteLogFile(ex.ToString());
            }
        }
Пример #10
0
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="dirName"></param>
        public void MakeDir(string dirName)
        {
            FtpWebRequest reqFTP;

            try
            {
                // dirName = name of the directory to create.
                reqFTP             = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + dirName));
                reqFTP.Method      = WebRequestMethods.Ftp.MakeDirectory;
                reqFTP.UseBinary   = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response  = (FtpWebResponse)reqFTP.GetResponse();
                Stream         ftpStream = response.GetResponseStream();

                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                //Insert_Standard_ErrorLog.Insert("FtpWeb", "MakeDir Error --> " + ex.Message);
                LogClass.WriteLogFile(ex.ToString());
            }
        }
Пример #11
0
        private void WorkThread()
        {
            try
            {
                string        root1 = AppDomain.CurrentDomain.BaseDirectory;
                DirectoryInfo di1   = new DirectoryInfo(root1);
                DirectoryInfo di2   = di1.Parent.Parent;
                string        root  = di2.FullName;
                //Directory.SetCurrentDirectory(root);
                bool update = false;
                int  i      = 0;
                foreach (var item in arrUpdate)
                {
                    //判断是否是目录
                    string ext = Path.GetExtension(item.ToString());
                    if (string.IsNullOrEmpty(ext))
                    {
                        if (!Directory.Exists(root + item.ToString()))
                        {
                            Directory.CreateDirectory(root + item.ToString());
                            this.Invoke((EventHandler) delegate
                            {
                                listBoxTip.Items.Add(item.ToString());
                            });
                        }
                    }
                    else
                    {
                        string path = Path.GetDirectoryName(item.ToString()) + "\\";
                        string name = Path.GetFileName(item.ToString());
                        if (File.Exists(path + item.ToString()))
                        {
                            File.Delete(path + item.ToString());
                        }
                        bool ret = ftp.Download(root, item.ToString());
                        if (ret)
                        {
                            this.Invoke((EventHandler) delegate
                            {
                                listBoxTip.Items.Add(item.ToString());
                            });
                        }
                        else
                        {
                            LogClass.WriteLogFile("download file error:" + item.ToString());
                        }
                    }
                    i++;
                    this.Invoke((EventHandler) delegate
                    {
                        progressBar1.Value = i;
                    });
                    listBoxTip.TopIndex = listBoxTip.Items.Count - 1;
                }
                this.Invoke((EventHandler) delegate
                {
                    MessageBox.Show("更新完成");
                    if (File.Exists(root1 + "UpdateVersion.xml"))
                    {
                        File.Delete(root1 + "UpdateVersion.xml");
                    }
                    Thread.Sleep(100);
                    File.Move(root1 + updateFile, root1 + "UpdateVersion.xml");


                    if (File.Exists(root1 + "BackUp/UpdateVersion.xml"))
                    {
                        File.Delete(root1 + "BackUp/UpdateVersion.xml");
                    }
                    File.Copy(root1 + "UpdateVersion.xml", root1 + "BackUp/UpdateVersion.xml");
                    if (isStartAfterUpdate == "1")
                    {
                        Process.Start(root1 + updateProcessPath);
                    }

                    this.Close();
                });
            }
            catch (Exception e)
            {
                LogClass.WriteLogFile("thread error:" + e.Message);
            }
        }
Пример #12
0
        private ArrayList ParseVersionFile(string file)
        {
            ArrayList arr  = new ArrayList();
            string    root = AppDomain.CurrentDomain.BaseDirectory;
            //string root = Environment.CurrentDirectory.ToString();
            DirectoryInfo di1 = new DirectoryInfo(root);
            DirectoryInfo di2 = di1.Parent.Parent;

            root = di2.FullName;
            try
            {
                verStdModle = ReadConfig(file);

                //判断文件主版本是否已经改变,需要更新
                if (verStdModle.Version != verModle.Version)
                {
                    foreach (var item in verStdModle.fileDic)
                    {
                        if (verStdModle.PubNumber == item.Value.pubnumber)
                        {
                            if (verModle.fileDic.ContainsKey(item.Key.ToString()))
                            {
                                if (verModle.fileDic[item.Key.ToString()].version != item.Value.version.ToString())
                                {
                                    arr.Add(item.Key.ToString());
                                }
                            }
                            else
                            {
                                arr.Add(item.Key.ToString());
                            }
                        }
                        else
                        {
                            //发布计数不相等,需要删除这个文件
                            string ext = Path.GetExtension(root + item.Key.ToString());
                            if (string.IsNullOrEmpty(ext))
                            {
                                if (Directory.Exists(root + item.Key.ToString()))
                                {
                                    DirectoryInfo di = new DirectoryInfo(root + item.Key.ToString());
                                    if (di.GetFiles().Length > 0)
                                    {
                                        //di.Delete(true);
                                    }
                                    else
                                    {
                                        //Directory.Delete(root + item.Key.ToString());
                                    }
                                }
                            }
                            else
                            {
                                if (File.Exists(root + item.Key.ToString()))
                                {
                                    //File.Delete(root + item.Key.ToString());
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogClass.WriteLogFile("ParseVersionFile error :" + e.Message);
            }
            return(arr);
        }