using FtpLib; using GeneralLib; using KingLib; using ProService; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; using System.Xml; using System.Xml.Serialization; using PowerPoint = Microsoft.Office.Interop.PowerPoint; namespace SunVoteARSPPT { public partial class FrmFTPServer : Form { public FrmFTPServer() { InitializeComponent(); } private void FrmPTFServer_Load(object sender, EventArgs e) { try { lstExamList.Items.Clear(); LstExam.Clear(); grpDownFileRoster.Enabled = grpDownFilePPT.Enabled = false; dgvExamSelect.ColumnHeadersVisible = false; dgvExamSave.ColumnHeadersVisible = false; //List d = new List(); //d.Add(new Item() { Name = "Sl No.", Value = "1" }); //d.Add(new Item() { Name = "Exam Unique Id", Value = "3110171" }); //d.Add(new Item() { Name = "Exam Name", Value = "Test Exam1 1" }); //d.Add(new Item() { Name = "Theme", Value = "Unit 1" }); //d.Add(new Item() { Name = "Faculty", Value = "Shri Purnesh Kumar" }); //d.Add(new Item() { Name = "Subject", Value = "Hindi Patrachar" }); //d.Add(new Item() { Name = "Batch", Value = "IFS (P) 2017-2019" }); //MyXmlSerializer.SerializerToFile(d, GlobalInfo.APP_DIR + @"\ExamInfo.xml"); //Item f = d.Find(delegate (Item a) //{ // return (a.a == "a"); //}); LoadFtpSet(); LoadExamSet(); dgvExamSave.CellEndEdit += DgvExamSave_CellEndEdit; PowerPoint.Presentation pres = Globals.SunVoteARSAddIn.Application.ActivePresentation; if (pres != null) { txtUpReportResult.Text = pres.FullName; } StartTimer(true); if (IsShowAutoUp) { tabFTP.SelectedTab = tabFtpServerUp; TmrAutoUp.Tick += TmrAutoUp_Tick; TmrAutoUp.Interval = 200; TmrAutoUp.Enabled = true; } //杨斌 2018-02-06 string dir = GlobalInfo.APP_DIR + @"\Reports\"; if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); } catch(Exception ex) { SystemLog.WriterLog(ex); } } private void FrmFTPServer_FormClosed(object sender, FormClosedEventArgs e) { StartTimer(false); //GlobalInfo.RunToolKit("RunBack"); //GlobalInfo.RefreshConnectState(); } System.Windows.Forms.Timer TmrAutoUp = new System.Windows.Forms.Timer(); private void TmrAutoUp_Tick(object sender, EventArgs e) { TmrAutoUp.Enabled = false; this.Activate(); btnUpload_Click(null, null); //Action action = (obj) => btnUpload_Click(null, null); //action.BeginInvoke(this, ar => action.EndInvoke(ar), null); } private void DgvExamSave_CellEndEdit(object sender, DataGridViewCellEventArgs e) { SaveExamSet(); } FtpWeb FTP = null; public static List GetExamInfo() { List res = new List(); if (GlobalInfo.OEMLogo == OEMLogos.oemEasyTest) { //string fileSave = GlobalInfo.APP_DIR + @"\Resources\SystemConfig\ExamInfo.xml"; //res = MyXmlSerializer.DisSerializerFromFile>(fileSave); PowerPoint.Presentation pres = Globals.SunVoteARSAddIn.Application.ActivePresentation; if (pres != null) { string xml = pres.Tags["ExamInfo"] + ""; res = MyXmlSerializer.Deserialize>(xml); } } return res; } bool IsLoad = false; private void LoadFtpSet() { IsLoad = true; txtFTPServerDown.Text = GlobalInfo.sysConfig.FtpServerIPDown; txtFtpServerUp.Text = GlobalInfo.sysConfig.FtpServerIPUp; chkAutoUpload.Checked = GlobalInfo.sysConfig.FtpAutoUp; chkUpReportDetail.Checked = GlobalInfo.sysConfig.FtpAutoUpReportDetail;//杨斌 2018-01-31 chkUpReportPart.Checked = GlobalInfo.sysConfig.FtpAutoUpReportParticipants;//杨斌 2018-02-05 IsLoad = false; } private void SaveFtpAutoUp() { GlobalInfo.sysConfig.FtpAutoUp = chkAutoUpload.Checked; INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH).WriteValue("FtpSet", "FtpAutoUp", GlobalInfo.sysConfig.FtpAutoUp); } private bool SaveFtpSetDown() { List lsRes = new FrmFTPPwd().ShowSet(this, GlobalInfo.sysConfig.FtpUserIDDown, GlobalInfo.sysConfig.FtpPasswordDown); if (lsRes.Count < 1) return false; GlobalInfo.sysConfig.FtpServerIPDown = txtFTPServerDown.Text; GlobalInfo.sysConfig.FtpUserIDDown = lsRes[0]; GlobalInfo.sysConfig.FtpPasswordDown = lsRes[1]; //修正不能保存将ReadString改成WriteValue。杨斌 2018-04-12 INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH).WriteValue("FtpSet", "FtpServerIPDown", GlobalInfo.sysConfig.FtpServerIPDown); INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH).WriteValue("FtpSet", "FtpUserIDDown", GlobalInfo.sysConfig.FtpUserIDDown); INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH).WriteValue("FtpSet", "FtpPasswordDown", GlobalInfo.sysConfig.FtpPasswordDown); return true; } private bool SaveFtpSetUp() { List lsRes = new FrmFTPPwd().ShowSet(this, GlobalInfo.sysConfig.FtpUserIDUp, GlobalInfo.sysConfig.FtpPasswordUp); if (lsRes.Count < 1) return false; GlobalInfo.sysConfig.FtpServerIPUp = txtFtpServerUp.Text; GlobalInfo.sysConfig.FtpUserIDUp = lsRes[0]; GlobalInfo.sysConfig.FtpPasswordUp = lsRes[1]; //修正不能保存将ReadString改成WriteValue。杨斌 2018-04-12 INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH).WriteValue("FtpSet", "FtpServerIPUp", GlobalInfo.sysConfig.FtpServerIPUp); INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH).WriteValue("FtpSet", "FtpUserIDUp", GlobalInfo.sysConfig.FtpUserIDUp); INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH).WriteValue("FtpSet", "FtpPasswordUp", GlobalInfo.sysConfig.FtpPasswordUp); return true; } private void InitFtpDown(string remotePath = "") { string url = GlobalInfo.sysConfig.FtpServerIPDown; if (string.IsNullOrEmpty(url)) url = "210.212.83.134/Exams"; if (url[url.Length - 1] == '/') url = url.Substring(0, url.Length - 1); //FTP = new FtpWeb(url, remotePath, @"ftpuser", @"spot$Lzm"); FTP = new FtpWeb(url, remotePath, GlobalInfo.sysConfig.FtpUserIDDown, GlobalInfo.sysConfig.FtpPasswordDown); } private void InitFtpUp() { string url = GlobalInfo.sysConfig.FtpServerIPUp; if (string.IsNullOrEmpty(url)) url = "210.212.83.134/Results"; if (url[url.Length - 1] == '/') url = url.Substring(0, url.Length - 1); //FTP = new FtpWeb(url, "", @"ftpuser", @"spot$Lzm"); FTP = new FtpWeb(url, "", GlobalInfo.sysConfig.FtpUserIDUp, GlobalInfo.sysConfig.FtpPasswordUp); } private void btnDownSet_Click(object sender, EventArgs e) { //保存设置 if (!SaveFtpSetDown()) return; //刷新列表 btnRefresh_Click(null, null); } private void btnDownloadRoster_Click(object sender, EventArgs e) { string fileSave = GlobalInfo.SaveFileDialog("(*.xlsx)|*.xlsx", GlobalInfo.APP_DIR + @"\Resources\RosterList\"); if (fileSave.Length < 1) return; lblFtpDownServerFail.Visible = false; btnDownloadRoster.Enabled = false; this.Cursor = Cursors.WaitCursor; try { string dirRemote = lstExamList.SelectedItem + ""; InitFtpDown(dirRemote); string dir = Path.GetDirectoryName(fileSave); string file = Path.GetFileName(fileSave); FTP.Download(dir, "Roster.xlsx", file); } catch (Exception ex) { lblFtpDownServerFail.Visible = true; //MessageBox.Show(ex + ""); } btnDownloadRoster.Enabled = true; this.Cursor = Cursors.Default; } private void btnDownloadPPT_Click(object sender, EventArgs e) { string fileSave = GlobalInfo.SaveFileDialog("(*.pptx)|*.pptx", GlobalInfo.APP_DIR); if (fileSave.Length < 1) return; lblFtpDownServerFail.Visible = false; btnDownloadPPT.Enabled = false; this.Cursor = Cursors.WaitCursor; try { string dirRemote = lstExamList.SelectedItem + ""; InitFtpDown(dirRemote); string dir = Path.GetDirectoryName(fileSave); string file = Path.GetFileName(fileSave); FTP.Download(dir, "Test.pptx", file); } catch (Exception ex) { lblFtpDownServerFail.Visible = true; //MessageBox.Show(ex + ""); } btnDownloadPPT.Enabled = true; this.Cursor = Cursors.Default; } List LstExam = new List(); private void btnRefresh_Click(object sender, EventArgs e) { lblFtpDownServerFail.Visible = false; btnRefresh.Enabled = false; this.Cursor = Cursors.WaitCursor; try { InitFtpDown(); LstExam.Clear(); lstExamList.Items.Clear(); txtSearch.Clear(); grpDownFileRoster.Enabled = grpDownFilePPT.Enabled = false; //Action action = (obj) => FTP.GetDirectoryList(); //IAsyncResult ares = action.BeginInvoke(this, ar => //{ // action.EndInvoke(ar); // string[] aa = ares.AsyncState as string[]; // foreach (var v in aa) // { // lstExamList.Items.Add(v); // LstExam.Add(v); // } //}, null); string[] a = FTP.GetDirectoryList(); foreach (var v in a) { lstExamList.Items.Add(v); LstExam.Add(v); } grpDownFileRoster.Enabled = grpDownFilePPT.Enabled = (lstExamList.SelectedIndex >= 0); } catch (Exception ex) { lblFtpDownServerFail.Visible = true; //MessageBox.Show(ex + ""); } btnRefresh.Enabled = true; this.Cursor = Cursors.Default; } private void StartTimer(bool isStart) { if (TmrRun == null) { TmrRun = new System.Timers.Timer(); TmrRun.AutoReset = true; TmrRun.Elapsed += TmrRun_Elapsed; TmrRun.Interval = 500; } TmrRun.Enabled = isStart; } long TimerCount = 0; System.Timers.Timer TmrRun = null; private void TmrRun_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Param pa = new Param(); Param p = GlobalInfo.ServerRequest(pa); //TimerCount++; //this.Text = "C=" + TimerCount; } private void btnSaveExamInfo_Click(object sender, EventArgs e) { dgvExamSave.Rows.Clear(); foreach (DataGridViewRow row in dgvExamSelect.Rows) { int i = dgvExamSave.Rows.Add(); for (int j = 0; j < dgvExamSelect.ColumnCount; j++) dgvExamSave.Rows[i].Cells[j].Value = row.Cells[j].Value; } SaveExamSet(); } private void SaveExamSet() { PowerPoint.Presentation pres = Globals.SunVoteARSAddIn.Application.ActivePresentation; if (pres == null) return; List lstExam = new List(); foreach (DataGridViewRow row in dgvExamSave.Rows) { lstExam.Add(new Item() { Name = row.Cells[0].Value + "", Value = row.Cells[1].Value + "" }); } string xml = MyXmlSerializer.Serializer(lstExam); pres.Tags.Add("ExamInfo", xml); ShowUpDetail(lstExam[1].Value, false); ShowUpPart(lstExam[1].Value, false); } private string ShowUpDetail(string examID, bool beShowTime) { string pathReportDetail = GlobalInfo.APP_DIR + @"\Reports\" + examID + "_Personal Achievement and Detail_" + (beShowTime ? DateTime.Now.ToString("yyyy-MM-dd_HHmmss") : "[yyyy-MM-dd_HHmmss]") + ".xlsx"; txtUpReportDetail.Text = pathReportDetail; return pathReportDetail; } private string ShowUpPart(string examID, bool beShowTime) { string pathReportPart = GlobalInfo.APP_DIR + @"\Reports\" + examID + "_Result by Participants_" + (beShowTime ? DateTime.Now.ToString("yyyy-MM-dd_HHmmss") : "[yyyy-MM-dd_HHmmss]") + ".xlsx"; txtUpReportPart.Text = pathReportPart; return pathReportPart; } private void LoadExamSet() { //加载模板 dgvExamSave.Rows.Clear(); string fileSave = GlobalInfo.APP_DIR + @"\Resources\SystemConfig\ExamInfo.xml"; List ls = MyXmlSerializer.DisSerializerFromFile>(fileSave); foreach (var v in ls) { dgvExamSelect.Rows.Add(v.Name, ""); dgvExamSave.Rows.Add(v.Name, ""); } //加载ppt保存的参数 PowerPoint.Presentation pres = Globals.SunVoteARSAddIn.Application.ActivePresentation; if (pres == null) return; txtUpReportDetail.Text = ""; txtUpReportPart.Text = ""; txtUpReportQues.Text = ""; string xml = pres.Tags["ExamInfo"]; List lstExam = MyXmlSerializer.Deserialize>(xml); if (lstExam.Count >= 2) { dgvExamSave.Rows.Clear(); foreach (var v in lstExam) { int i = dgvExamSave.Rows.Add(v.Name, v.Value); } ShowUpDetail(lstExam[1].Value, false); ShowUpPart(lstExam[1].Value, false); } } private void btnUpSet_Click(object sender, EventArgs e) { //保存设置 if (!SaveFtpSetUp()) return; //尝试连接 lblFtpUpServerFail.Visible = false; btnUpSet.Enabled = false; this.Cursor = Cursors.WaitCursor; try { InitFtpUp(); lblFtpUpServerFail.Visible = !FTP.TryTestConnect(); } catch (Exception ex) { lblFtpUpServerFail.Visible = true; //MessageBox.Show(ex + ""); } btnUpSet.Enabled = true; this.Cursor = Cursors.Default; } private bool IsShowAutoUp = false; public void ShowAutoUp() { IsShowAutoUp = true; this.TopMost = true; this.Show(); //this.ShowDialog(); } private void btnUpload_Click(object sender, EventArgs e) { PowerPoint.Presentation pres = Globals.SunVoteARSAddIn.PPTEdit.PPT.ActivePresentation; if (pres == null) return; bool isUpDetail = chkUpReportDetail.Checked; bool isUpPart = chkUpReportPart.Checked;//杨斌 2018-02-05 progressBar.Value = 0; progressBar.Minimum = 0; progressBar.Maximum = 3; if (isUpDetail) progressBar.Maximum += 2; if (isUpPart) progressBar.Maximum += 2; progressBar.Visible = true; Application.DoEvents(); List lstExam = GetExamInfo(); if (lstExam.Count < 2) { MessageBox.Show("Please first save to or edit local exam set."); tabFTP.SelectedTab = tabFtpServerDown; return; } lblFtpUpServerFail.Visible = false; btnUpload.Enabled = false; this.Cursor = Cursors.WaitCursor; try { string examID = lstExam[1].Value; string file = GlobalInfo.APP_DIR + @"\Resources\" + examID + "_" + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + ".arsz"; string fileUp = PPTOper.Exportmdb(file, pres);//生成备份文件 string dirRemote = lstExamList.SelectedItem + ""; progressBar.Value++; int errFTP = 0; //上传数据文件 try { InitFtpUp(); if (FTP.Upload(fileUp)) { progressBar.Value++; } else { errFTP++; lblFtpUpServerFail.Visible = true; } } catch (Exception ee) { SystemLog.WriterLog(ee, false); } if (isUpDetail && (errFTP < 1))//杨斌 2018-01-31 { //生成报表。 string pathReportDetail = ShowUpDetail(lstExam[1].Value, true); ReportEx.ExportReportScore(GlobalInfo.response.ShowNameCol, pathReportDetail); progressBar.Value++; //上传报表。 try { InitFtpUp(); if (FTP.Upload(pathReportDetail)) { progressBar.Value++; } else { errFTP++; lblFtpUpServerFail.Visible = true; } } catch (Exception ee) { SystemLog.WriterLog(ee, false); } } if (isUpPart && (errFTP < 1))//杨斌 2018-02-05 { //生成报表。 string pathReportPart = ShowUpPart(lstExam[1].Value, true); ReportEx.ExportReportVoter(pathReportPart); progressBar.Value++; //上传报表。 try { InitFtpUp(); if (FTP.Upload(pathReportPart)) { progressBar.Value++; } else { errFTP++; lblFtpUpServerFail.Visible = true; } } catch (Exception ee) { lblFtpUpServerFail.Visible = true; SystemLog.WriterLog(ee, false); } } //删除上传文件 try { File.Delete(fileUp); progressBar.Value++; } catch(Exception ee) { SystemLog.WriterLog(ee, false); } } catch (Exception ex) { //MessageBox.Show(ex + ""); this.TopMost = false; SystemLog.WriterLog(ex);//这里因为是顶层窗体,MessageBox会显示在窗体后面 } btnUpload.Enabled = true; this.Cursor = Cursors.Default; GlobalInfo.RunToolKit("RunBack"); GlobalInfo.RefreshConnectState(); } private void lstExamList_SelectedIndexChanged(object sender, EventArgs e) { grpDownFileRoster.Enabled = grpDownFilePPT.Enabled = (lstExamList.SelectedIndex >= 0); ShowExamInfo(); } void ShowExamInfo() { lblFtpDownServerFail.Visible = false; lstExamList.Enabled = false; this.Cursor = Cursors.WaitCursor; try { dgvExamSelect.Rows.Clear(); string dirRemote = lstExamList.SelectedItem + ""; InitFtpDown(dirRemote); string fileSave = GlobalInfo.APP_DIR + @"\Resources\ExamInfo.xml"; string dir = Path.GetDirectoryName(fileSave); string file = Path.GetFileName(fileSave); FTP.Download(dir, "ExamInfo.xml", file); List ls = MyXmlSerializer.DisSerializerFromFile>(fileSave); foreach (var v in ls) dgvExamSelect.Rows.Add(v.Name, v.Value); } catch (Exception ex) { lblFtpDownServerFail.Visible = true; //MessageBox.Show(ex + ""); } lstExamList.Enabled = true; this.Cursor = Cursors.Default; } private void txtSearch_TextChanged(object sender, EventArgs e) { lstExamList.Items.Clear(); if (string.IsNullOrEmpty(txtSearch.Text)) { foreach (var v in LstExam) lstExamList.Items.Add(v); } else { foreach (string v in LstExam) { if (v.IndexOf(txtSearch.Text) >= 0) lstExamList.Items.Add(v); } } } private void chkAutoUpload_CheckedChanged(object sender, EventArgs e) { if (IsLoad) return; SaveFtpAutoUp(); } private void chkUpReportDetail_CheckedChanged(object sender, EventArgs e) { //杨斌 2018-01-31 if (IsLoad) return; GlobalInfo.sysConfig.FtpAutoUpReportDetail = chkUpReportDetail.Checked; INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH).WriteValue("FtpSet", "FtpAutoUpReportDetail", GlobalInfo.sysConfig.FtpAutoUpReportDetail); } private void chkUpReportPart_CheckedChanged(object sender, EventArgs e) { //杨斌 2018-01-31 if (IsLoad) return; GlobalInfo.sysConfig.FtpAutoUpReportParticipants = chkUpReportPart.Checked; INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH).WriteValue("FtpSet", "FtpAutoUpReportParticipants", GlobalInfo.sysConfig.FtpAutoUpReportParticipants); } } public class Item { public string Name = ""; public string Value = ""; } }