I have written a program that is very simple, with a very specific purpose. However, having written it in approximately 2 hours, (with a bit of cut/paste from some of my other programs and MSDN) it's very ugly.
For example, the main part of the program is held entirely within one big using
block, when it really doesn't need to be.
Also, while for the initial purpose of this program is to create placeholder files, the current .pdf files are not in the correct format, which is fine. However, is it possible to create pdf's in the correct format?
Can anyone give me any suggestions on how to neaten this up?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.VisualBasic.FileIO;
namespace File_Creation_Program
{
class Program
{
[STAThread]
static void Main(string[] args)
{
// Clears console in case program has been re-run
Console.Clear();
List<string> fileNames = new List<string>();
Console.WriteLine("Please select 1 or more CSV files to be converted for fle creation.");
Console.WriteLine();
// "Multiselect" prperty allows more than 1 file to be selected at a time.
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
openFileDialog1.Title = "Open file";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "csv";
openFileDialog1.Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialog1.Multiselect = true;
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
// If the search for the file is OK (i.e. the file exists), stores the filename in a string.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Console.WriteLine("Working...");
Console.WriteLine();
// Splits each file by line (as a precaution, as no file should have no more than one line)
foreach (string f in openFileDialog1.FileNames)
{
string[] importFile = File.ReadAllText(f).Split('\n');
foreach (string line in importFile)
{
string[] newFiles = line.Split(',');
Console.WriteLine("Please choose where you would like to save the files from " + f);
Console.WriteLine();
FolderBrowserDialog saveFile = new FolderBrowserDialog();
if (saveFile.ShowDialog() == DialogResult.OK)
{
string folderName = saveFile.SelectedPath;
// Creates the placeholder file. Since the files are created from a string, the format is incorrect,
// as it is effectively the same as changing the extension from a ".txt" to a ".pdf".
// Therefore, the file is useless (appears "corrupt") as anything but a placeholder.
foreach (string s in newFiles)
{
Console.WriteLine("Working...");
Console.WriteLine();
try
{
StreamWriter sw;
string fileName = folderName + "\\" + s;
sw = File.CreateText(fileName);
sw.WriteLine("Place holder file for Hard Copy entry");
sw.Close();
}
catch (Exception e) { Console.WriteLine("File not created - " + e.Message); }
}
Console.WriteLine("...Done!");
Console.WriteLine();
}
}
}
}
}
// In the case that more files need to be created.
Console.WriteLine("Would you like to create more files? Y/N");
string choice = Console.ReadLine();
Program.Return(choice, args);
}
static void Return(string option, string[] args)
{
switch (option.ToUpper())
{
case "Y":
case "YES":
// Program restarts
Main(args);
break;
case "N":
case "NO":
// Program ends
break;
default:
Console.WriteLine("Not a valid input... Please enter 'Yes' or 'No'");
option = Console.ReadLine();
Return(option, args);
break;
}
}
}
}