// public void ReloadFile() // { // // Reload file if selected file is valid // if (!string.IsNullOrEmpty(this.currentFilePath)) // { // // Read from file // string content = FileHelper.ReadFile(this.currentFilePath, true, // MyConfig.GetSetting(MyConfig.Key_StudentName)); // // // Load it to Text // tbCode.Text = content; // lblFileName.Text = Path.GetFileName(this.currentFilePath); // } // else // { // tbCode.Text = ""; // lblFileName.Text = "(select a file from exercise list)"; // } // } public void LoadFileLists() { string[] list = FileHelper.ListFiles(MyFileHelper.GetSubfolderPath(MyFileHelper.FOLDER_COMPULSORY)); lvCompulsory.View = View.Details; lvCompulsory.Columns.Add("FileName", -2); lvCompulsory.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None; lvCompulsory.Items.Clear(); foreach (string file in list) { string name = Path.GetFileName(file); ListViewItem item = lvCompulsory.Items.Add(new ListViewItem(new string[] { name })); item.Tag = file; } list = FileHelper.ListFiles(MyFileHelper.GetSubfolderPath(MyFileHelper.FOLDER_ADDITIONAL)); lvAdditional.View = View.Details; lvAdditional.Columns.Add("FileName", -2); lvAdditional.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None; lvAdditional.Items.Clear(); foreach (string file in list) { string name = Path.GetFileName(file); ListViewItem item = lvAdditional.Items.Add(new ListViewItem(new string[] { name })); item.Tag = file; } }
public void AddFileToAdditionalListView(string inputFile) { // Copy the file into "additional" folder string fileName = Path.GetFileName(inputFile); string destFile = Path.Combine(MyFileHelper.GetSubfolderPath(MyFileHelper.FOLDER_ADDITIONAL), fileName); bool exists = File.Exists(destFile); if (exists) { DialogResult answer = MessageBox.Show($"Overwrite existing file: {fileName}?", "Overwrite File", MessageBoxButtons.YesNo); if (answer == DialogResult.No) { return; } } string content = FileHelper.ReadFile(inputFile); FileHelper.WriteFile(destFile, content, true, MyConfig.GetSetting(MyConfig.Key_StudentHash)); tabFileLists.SelectTab(1); ListViewItem item = null; if (!exists) { item = lvAdditional.Items.Add(new ListViewItem(new string[] { fileName })); item.Tag = destFile; } else { item = lvAdditional.FindItemWithText(fileName); } if (item != null) { UpdateWithListItem(item); lvAdditional.SelectedItems.Clear(); lvAdditional.Items[item.Index].Focused = true; lvAdditional.Items[item.Index].Selected = true; } }
private void btNewFile_Click(object sender, EventArgs e) { string filename = Interaction.InputBox("Enter file name", "Create New File", "myfile.c", -1, -1); if (string.IsNullOrEmpty(filename.Trim())) { return; } if (!filename.EndsWith(".c")) { filename = filename + ".c"; } filename = FileHelper.SanitizeFileName(filename); if (!string.IsNullOrEmpty(filename)) { // Create new file and add to additional_list string toFolder = MyFileHelper.GetSubfolderPath(MyFileHelper.FOLDER_ADDITIONAL); string filePath = Path.Combine(toFolder, filename); if (File.Exists(filePath)) { MessageBox.Show($"File name already exists: {filename}", "Create File Failed", MessageBoxButtons.OK); return; } if (!string.IsNullOrEmpty(filePath)) { FileHelper.WriteFile(filePath, ""); tabFileLists.SelectTab(1); ListViewItem item = lvAdditional.Items.Add(new ListViewItem(new string[] { filename })); item.Tag = filePath; lvAdditional.SelectedItems.Clear(); lvAdditional.Items[item.Index].Focused = true; lvAdditional.Items[item.Index].Selected = true; } } }
private void BtExport_Click(object sender, EventArgs e) { SaveFileDialog dialog = new SaveFileDialog(); dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); dialog.DefaultExt = ".zip"; // Default file extension dialog.Filter = "zip files (*.zip)|*.zip|All files (*.*)|*.*"; string hash = MyConfig.GetSetting(MyConfig.Key_StudentHash); dialog.FileName = hash + ".zip"; DialogResult result = dialog.ShowDialog(this); if (result != DialogResult.OK || string.IsNullOrEmpty(dialog.FileName)) { return; } try { if (File.Exists(dialog.FileName)) { File.Delete(dialog.FileName); } string zipPath = dialog.FileName; string userFolder = MyFileHelper.GetSubfolderPath(); ZipFile.CreateFromDirectory(userFolder, zipPath); MessageBox.Show("Exported: " + dialog.FileName, "Export Profile Successful", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Export Profile Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } }