So lets have a try. My first console application and I want to know if its good or bad.
The code should do this:
- You have a bunch of zipped folders in a directory
- You extract them.
- Get a specific XML file where a specific URL Link is inside
- Get the two files from the URL Link (extension .ere and .ter)
- Put them into the extracted folder
Zip the new folder with the new files
namespace MyApp { class Program { static void Main(string[] args) { try { DirectoryAvailable(args[0]); //serverpath } catch (Exception e) { Console.WriteLine(e.Message); DisplayHelp(); } Environment.Exit(0); } public static void DirectoryAvailable(string server) { CreateDirectory(server); GoThroughFolder(server); DeleteCreateOutputFolder(server); numberOfZip = Directory.GetFiles(server + @"\OutputZip").Length; Console.Write("Job done"); Console.ReadLine(); } public static void GoThroughFolder(string s) { string[] files = Directory.GetFiles(s, "BGBLA*" + ".zip", SearchOption.AllDirectories); foreach (var item in files) { string outputPath = s + @"\Output\"; string outputPathZip = s + @"\OutputZip\"; string getBGBLA = Path.GetFileNameWithoutExtension(item); string getXmlPath = string.Empty; UnzipFile(item, outputPath + getBGBLA); outputPath += getBGBLA; if (string.IsNullOrEmpty(Path.GetExtension(outputPath))) getXmlPath = outputPath + @"\" + getBGBLA + ".xml"; GetHtmlFile(getXmlPath, outputPath); ZipFilePackaging(outputPath, outputPathZip + getBGBLA + ".zip"); Console.WriteLine("Info: " + outputPathZip + getBGBLA + ".zip"); } } private static void UnzipFile(string zipFile, string outputPath) { try { ZipFile.ExtractToDirectory(zipFile, outputPath); } catch (Exception e) { Console.WriteLine(e.Message); Environment.Exit(0); } } private static void ZipFilePackaging(string zipFile, string outputPath) { try { ZipFile.CreateFromDirectory(zipFile, outputPath); } catch (Exception e) { Console.WriteLine(e.Message); Environment.Exit(0); } } private static void GetHtmlFile(string s, string outputPath) { //XML variablen string selectedNodeToElement = @"//ns:OgdDocumentReference/ns:Data/ns:Dokumentliste/ns:ContentReference/ns:Urls/ns:ContentUrl/ns:Url"; string dokumentName = string.Empty; string hauptDokumentXml = "Hauptdokument.xml"; //File-Exts string htmlExtension = ".html"; string erechtExtension = ".ere"; string xmldsigExtension = ".ter"; string cooFileName = "COO"; //vars for download files string eRechtDownloadLink = string.Empty; string eRechtFileName = string.Empty; string dSigDownloadLink = string.Empty; string dSigFileName = string.Empty; //XML laden XmlDocument doc = new XmlDocument(); doc.Load(s); //set namespace string currentNamespace = doc.DocumentElement.FirstChild.NamespaceURI; XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("ns", currentNamespace); foreach (XmlNode htmlUrlMain in doc.DocumentElement.SelectNodes(selectedNodeToElement, ns)) { //catch main document if (htmlUrlMain.InnerText == hauptDokumentXml) { dokumentName = Path.GetFileNameWithoutExtension(htmlUrlMain.InnerText); } //check if html exists if (Path.GetExtension(htmlUrlMain.InnerText) == htmlExtension) { //check if ioo exists if (htmlUrlMain.InnerText.Contains(cooFileName)) { dokumentName = Path.GetFileNameWithoutExtension(htmlUrlMain.InnerText); } //*.ere eRechtDownloadLink = GenerateErechtLink(htmlUrlMain.InnerText); eRechtFileName = outputPath + @"\" + dokumentName + erechtExtension; DownloadFile(eRechtDownloadLink, eRechtFileName); //*ter dSigDownloadLink = GenerateDsigLink(htmlUrlMain.InnerText); dSigFileName = outputPath + @"\" + dokumentName + xmldsigExtension; DownloadFile(dSigDownloadLink, dSigFileName); } } } private static string GenerateErechtLink(string urlToErechtFile) { try { var res = Path.ChangeExtension(urlToErechtFile, "erecht"); return res; } catch (Exception e) { Console.WriteLine(e.Message); throw; } } private static string GenerateDsigLink(string urlToXmldsigFile) { try { var res = Path.ChangeExtension(urlToXmldsigFile, "xmldsig"); return res; } catch (Exception e) { Console.WriteLine(e.Message); throw; } } private static void DownloadFile(string url, string pathName) { try { var client = new WebClient(); client.DownloadFile(url, pathName); } catch (Exception e) { Console.WriteLine(e.Message); throw; } } private static void DeleteCreateOutputFolder(string serverpath) { Directory.Delete(serverpath + @"\Output\", true); } private static void CreateDirectory(string server) { try { Directory.CreateDirectory(server + @"\Output"); Directory.CreateDirectory(server + @"\OutputZip"); } catch (Exception e) { Console.WriteLine(e.Message); throw; } } } }
So my problems I have to deal with:
- I am mixing english and german (vars, comments, names, ..). How to deal with it when german is main?
- I am not sure if this is the best way to do it? Maybe there is something, where I can extract, add files, zip again in one method/lib/...
- I have problems with setting try/catch. I don't know where I have to set it exactly, in my first tries I have the try/catch phrase everywhere...
- Mixing public/private. In this project, I wasn't sure what I should use in the project (if it grows)
- Declaring variables: Should each string be defined? Or just when I am using it more than once. Actual I have a mix. :(