Ejemplo n.º 1
0
        private void btnSaveFolder_Click(object sender, EventArgs e)
        {
            if (cbFtpSave.Checked)
            {
                FTPSettingForm fsf = new FTPSettingForm();
                if (fsf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    txtSaveFolder.Text = fsf.SelPath;
                    ftpSave            = fsf.FTPObj;
                }
                return;
            }
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if (!string.IsNullOrEmpty(txtSaveFolder.Text))
            {
                fbd.SelectedPath = txtSaveFolder.Text;
            }
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtSaveFolder.Text = fbd.SelectedPath;
            }
        }
Ejemplo n.º 2
0
        //生成文件
        private void GenerateFiles(DataTable dt, int count, ref string errInfo)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string savePath = dt.Rows[i]["SavePath"].ToString();
                string filePath = dt.Rows[i]["OriginalPath"].ToString();
                //FTP读取
                if (cbFtpSel.Checked)
                {
                    long     fileSize    = ftpSel.GetFileSize(filePath);
                    DateTime lastUpdTime = ftpSel.GetFileUpdateTime(filePath);
                    //FTP->FTP
                    if (cbFtpSave.Checked)
                    {
                        ftpSel.ChangeServerIp(filePath);
                        ftpSave.ChangeServerIp(savePath);
                        long saveSize = ftpSel.GetFileSize(savePath);
                        if (saveSize != -1)
                        {
                            DateTime saveLastUpdTime = ftpSave.GetFileUpdateTime(savePath);
                            if (saveSize == fileSize && saveLastUpdTime == lastUpdTime)
                            {
                                continue;
                            }
                        }
                        FtpHelper.Download(ftpSel, ftpSave, ref errInfo);
                    }
                    //FTP->本地
                    else
                    {
                        if (File.Exists(savePath))
                        {
                            FileInfo fi = new FileInfo(savePath);
                            if (fi.Length == fileSize && fi.LastWriteTime == lastUpdTime)
                            {
                                continue;
                            }
                            if (fi.Attributes == FileAttributes.ReadOnly)
                            {
                                fi.Attributes = FileAttributes.Normal;
                            }
                        }
                        ftpSel.Download(filePath, savePath, ref errInfo);
                    }
                }
                //本地读取
                else if (!cbFtpSel.Checked)
                {
                    FileInfo fi = new FileInfo(filePath);
                    //只读文件无法操作会抛出异常
                    var fiAttributes = fi.Attributes;
                    if (fiAttributes == FileAttributes.ReadOnly)
                    {
                        fi.Attributes = FileAttributes.Normal;
                    }

                    //本地->FTP
                    if (cbFtpSave.Checked)
                    {
                        //获得父路径 提取列表  判断是否存在  截取文件名和大小
                        long saveSize = ftpSave.GetFileSize(savePath);
                        if (saveSize != -1)//文件存在
                        {
                            DateTime lastUpdTime = ftpSave.GetFileUpdateTime(savePath);
                            if (fi.Length == saveSize && fi.LastWriteTime == lastUpdTime)
                            {
                                fi.Attributes = fiAttributes;
                                continue;
                            }
                        }
                        ftpSave.Upload(filePath, savePath, ref errInfo);
                    }
                    //本地->本地
                    else
                    {
                        if (File.Exists(savePath))
                        {
                            FileInfo newFi = new FileInfo(savePath);
                            //文件修改日期和大小相同  跳过 复原权限
                            if (newFi.Length == fi.Length && newFi.LastWriteTime == fi.LastWriteTime)
                            {
                                fi.Attributes = fiAttributes;
                                continue;
                            }
                        }
                        File.Copy(filePath, savePath, true);
                    }
                    //还原文件权限
                    fi.Attributes = fiAttributes;
                }
                if (errInfo == string.Empty)
                {
                    ++count;
                    lblPoint.Invoke(new Action(delegate
                    {
                        dataGridView1.Rows[i].Cells["IsSync"].Value = true;
                        lblPoint.Text = count.ToString();
                    }));
                }
            }
        }