Need your help guys, as you can see from the code below, I'm not a coder/programmer lol, but I need you to create this "Utilities" DLL to be used by a console application that simply calls Utilities.FileTasks();
I'm having some problems on declaring variables and initiating what type of methods (public static void, static void, public void, public string, etc). I just want to be able to create "global variables" (I know this is a bad example of coding) so that I'm able to use the same variable on different places, instead of declaring the same variable over and over again, within the methods that I want to develop.
May I have a little help please?
public class Utilities
{
//Search Directory
DirectoryInfo di { get; set; }
//newName should be a var type variable
EnvironmentVariableTarget newName { get; set; }
string version { get; set; }
void ZippingFile(string path)
{
System.IO.Compression.ZipFile.ExtractToDirectory(@"C:\Users\developer\Desktop\" + newName, @"C:\Users\developer\Desktop\TempExtract\XPTO");
System.IO.Compression.ZipFile.ExtractToDirectory(@"C:\Users\developer\Desktop\TempExtract\XPTO\payload_" + version, @"C:\Users\developer\Desktop\TempExtract\XPTO\payload\");
}
public static void FileTasks()
{
//Search Directory
DirectoryInfo di = new DirectoryInfo(@"C:\Users\developer\Desktop\");
//Get file name
FileInfo[] files = di.GetFiles("XPTO*.exe", SearchOption.TopDirectoryOnly);
//Convert to string
string FileName = files[0].ToString();
//Output
Console.WriteLine("Fetching file name: {0}", FileName);
//File rename
var sourcePath = @"C:\Users\developer\Desktop\" + FileName;
var newName = FileName + ".zip";
var directory = Path.GetDirectoryName(sourcePath);
var destinationPath = Path.Combine(directory, newName);
File.Move(sourcePath, destinationPath);
Console.WriteLine("File renamed to: " + newName);
//Create directory and extract Zip
string TempExtract = "C:\\Users\\developer\\Desktop\\TempExtract\\";
if (Directory.Exists(TempExtract) && (newName.StartsWith("XPTO_")))
{
Directory.Delete(TempExtract, true);
Console.WriteLine("Directory existed and it was deleted");
Directory.CreateDirectory(TempExtract + "\\XPTO");
Console.WriteLine("Directory " + TempExtract + "\\XPTO" + " was created");
}
else if (Directory.Exists(TempExtract) && (newName.StartsWith("XPTO2_")))
{
Directory.Delete(TempExtract, true);
Console.WriteLine("Directory existed and it was deleted");
Directory.CreateDirectory(TempExtract + "\\XPTO2");
Console.WriteLine("Directory " + TempExtract + "\\XPTO2" + " was created");
}
else if (newName.StartsWith("XPTO_"))
{
Console.WriteLine("Directory doesn't exist");
Directory.CreateDirectory(TempExtract + "\\XPTO");
Console.WriteLine("Directory " + TempExtract + "\\XPTO" + " was created");
}
else if (newName.StartsWith("XPTO2_"))
{
Console.WriteLine("Directory doesn't exist");
Directory.CreateDirectory(TempExtract + "\\XPTO2");
Console.WriteLine("Directory " + TempExtract + "\\XPTO2" + " was created");
}
if (newName.StartsWith("XPTO_"))
{
ZippingFile(@"C:\Users\developer\Desktop\XPTO");
}
else if (newName.StartsWith("XPTO2_"))
{
ZippingFile(@"C:\Users\developer\Desktop\XPTO2");
}
}
}