private void chkFileGroup_CheckedChanged(object sender, EventArgs e) { //skip extension validation for boilerplate, in case the extension is something like CSS (which doesn't have bp, but HTML does) //this may need to be expanded on if groups are added that have no boilerplate for any extension if (FileGroup.HasFileGroup(cboExtensions.Text) && chkFileGroup.Checked == true) { ToggleBoilerPlate(true); } //else, re-validate extension else { ToggleBoilerPlate(Boilerplate.HasBoilerplate(cboExtensions.Text)); } //change text for button + checkbox btnGenerate.Text = chkFileGroup.Checked ? "&Create Files" : "&Create File"; chkOpenFiles.Text = chkFileGroup.Checked ? "&Open Files When Complete" : "&Open File When Complete"; }
private void MakeFiles(List <string> extensionList = null) { //function will run once per item in file group (or just once if there is no group) string extension = extensionList == null?cboExtensions.Text.Trim() : extensionList[0]; string filePath = txtFilePath.Text.Trim(); string fileName = txtFileName.Text.Trim(); try { //create file string path = $"{filePath}/{fileName}.{extension}"; if (File.Exists(path)) { if (MessageBox.Show($"{fileName}.{extension} already exists in this directory. Would you like to replace it?", "Error", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } FileStream fs = File.Create(path); //if boilerplate is checked and valid, generate it and add to file if (chkBoilerplate.Checked && Boilerplate.HasBoilerplate(extension)) { string bp = Boilerplate.CreateBoilerplate(extension, fileName, chkFileGroup.Checked); UTF8Encoding encoding = new UTF8Encoding(); fs.Write(encoding.GetBytes(bp), 0, encoding.GetByteCount(bp)); } fs.Close(); //attempt to open new file if needed if (chkOpenFiles.Checked) { try { Process.Start(path); } catch (Exception) { MessageBox.Show("Unable to open file. Check your selected directory to verify the file exists.", "Error"); } } AddMessage($"{fileName}.{extension} created in {filePath}"); //if file group, repeat function for next file. stop the process if the list is empty if (extensionList != null && extensionList.Count() > 1) { MakeFiles(extensionList.GetRange(1, extensionList.Count() - 1)); } } catch (Exception ex) { MessageBox.Show("Unable to create file. Verify file extension and path are valid.", "Error"); } }
//checks user input; enables boilerplate checkbox where supported private void cboExtensions_TextUpdate(object sender, EventArgs e) { ToggleBoilerPlate(Boilerplate.HasBoilerplate(cboExtensions.Text)); ToggleFileGroup(FileGroup.HasFileGroup(cboExtensions.Text)); }