-9

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");
        }


    }
}
kenzoviski
  • 81
  • 1
  • 10
  • 1
    "newName should be a var type variable". 'Twas brillig. Can you clarify that? Can you also clarify exactly which parts of your code you're having trouble with? – 15ee8f99-57ff-4f92-890c-b56153 May 08 '17 at 17:03
  • 1
    If you are talking about the directories paths, I suggest you use a configuration file: http://stackoverflow.com/questions/13043530/what-is-app-config-in-c-net-how-to-use-it – Mishka May 08 '17 at 17:07
  • Here is a link to a tutorial on global variables. https://www.youtube.com/watch?v=kJ_pTzMoSo0 It took me 10 seconds to Google it. Please try to do more research before posting questions. There is tons of help out there for beginner programmers. If you are really truly lost this early in the game I would recommend taking a crash course to familiarize yourself with the basics. – brandonstrong May 08 '17 at 17:33
  • 1
    Please see: [What is the XY problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/233676) – EJoshuaS - Stand with Ukraine May 08 '17 at 18:09

1 Answers1

1

This seems like a classic case of the XY problem.

This is a really bad way to save yourself a little typing. It won't even save you much typing if you really look at it, either; you'll still have to do assignment. So the difference is literally:

public class SomeClass {
  public string abc;

  public foo() {
     abc = "something";
  )

}

vs.

public class SomeClass {
     public foo() {
        var abc = "something";
     )
}

So it doesn't even save you all that much typing. The actual declaration of the variable is hardly any longer than the assignment of the variable, which this won't prevent you from needing to do.

A few other points here:

First, strictly speaking, C# doesn't have "global variables" in the same way that C and C++ do. You can write things that are effectively global variables (with a few caveats), though (public static variables and Singleton classes).

With that said, using either of those in the first place is usually a terrible practice; both Singletons and global variables are an antipattern in the vast majority of cases (except where you're dealing with a case where there really and truly is only one of a particular resource and you have no choice - e.g. if you're interacting with a physical device and there's only one of it). It can quickly become very difficult to track the state of the variables and is practically begging for unmaintainable spaghetti code.

Another problem: what you're proposing can increase memory consumption because the object in the global variable can't be garbage-collected unless you explicitly set its value to null (which would increase the amount of typing over what you would've had to do if you just made it a local variable in the first place). If you're just doing this with one or two strings, it's probably not a big deal in terms of memory consumption, but it can become a huge deal if you're doing it with larger objects, a large number of objects, or objects that hold onto shared, limited, or unmanaged resources.

Which brings me back to my original point: don't do it. Just don't. It's a really bad way to save a very small amount of typing.