1

How can I return multiple Arrays with different Values? In my first function I get all files that are in my Folder.

In my second function I extract for each file the "modDesc.xml" and get my Information out of it. Now i want to return for each files a array with all this informations! But i dont know how.. I hope someone can help me!

Here is my code:

public string[] openDirectory(string DirectoryPath)
{
   string[] files = Directory.GetFiles(DirectoryPath, "*.zip");

   return files;
}


public string[] getModDesc(string DirectoryPath)
{
    string[] files = openDirectory(DirectoryPath);

    foreach (var file in files)
    {
        using (ZipFile zip = ZipFile.Read(file))
        {
            ZipEntry modDescHandler = zip["modDesc.xml"];

            if (modDescHandler != null)
            {
                if (File.Exists("tmp\\modDesc.xml"))
                {
                    File.Delete("tmp\\modDesc.xml");
                }

                modDescHandler.Extract("tmp");

                XDocument modDesc = XDocument.Load("tmp\\modDesc.xml");
                string modTitle = null;
                string modAuthor = null;
                string modVersion = null;
                string modFileName = null;

                try
                {
                    modTitle = modDesc.Element("modDesc").Element("title").Element("de").Value;
                    modAuthor = modDesc.Element("modDesc").Element("author").Value;
                    modVersion = modDesc.Element("modDesc").Element("version").Value;
                }

                catch
                {

                }

                modFileName = Path.GetFileName(file);

                string[] modInformation = { modTitle, modAuthor, modVersion, modFileName };

                File.Delete("tmp\\modDesc.xml");

                return modInformation;

            }
        }
    }

    return new string[0];
}

1 Answer 1

1

You could return a List<string[]> (i.e. a list of arrays) which would contain your collection of arrays for each file :

public List<string[]> getModDesc(string DirectoryPath)
{
       // Create a list to store your arrays
       List<string[]> fileInformation = new List<string[]>();

       // Get your files
       string[] files = openDirectory(DirectoryPath);
       foreach (var file in files)
       {
              using (ZipFile zip = ZipFile.Read(file))
              {
                    // All your code (omitted for brevity)

                    // Create your array for this file
                    string[] modInformation = { modTitle, modAuthor, modVersion, modFileName }; 

                    // Add this to your list
                    fileInformation.Add(modInformation);
              }
        }
        // At this point your arrays collection should have all of your 
        // arrays, so return it
        return fileInformation;
    }
}

Or if your file names were each unique and you wanted to make accessing them a bit easier, you could store them in a Dictionary that would allow you to look each one up by it's name :

public Dictionary<string,string[]> getModDesc(string DirectoryPath)
{
   // Create a list to store your arrays
   Dictionary<string,string[]> fileInformation = new Dictionary<string,string[]>();

   // Get your files
   string[] files = openDirectory(DirectoryPath);
   foreach (var file in files)
   {
          using (ZipFile zip = ZipFile.Read(file))
          {
                // All your code (omitted for brevity)

                // Create your array for this file
                string[] modInformation = { modTitle, modAuthor, modVersion, modFileName }; 

                // Add this to your dictionary, mapping the file name
                // to it's information
                fileInformation.Add(modFileName,modInformation);
          }
    }
    // At this point your dictionary should have all of your 
    // arrays, so return it
    return fileInformation;
}

}

Then, if you wanted to access the information from a file in your dictionary, you could simply use :

string[] information = yourDictionary["YourFileName"];
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you! I think it works.. :P How can I get the value from the list?
You could either iterate through it using a loop, or use yourList.ElementAt(index) to pull a specific index. Additionally, if you wanted to just return an array of arrays, you could change the return type of your method to string[][] and just change return fileInformation to return fileInformation.ToArray(), then you could reference you values by their index (like you would in any other array).
I have my foreach loop foreach(var file in fileInformaton) and now I want to get the information from the array to another list var list = new Mod { isModEnabled = true, modTitle = file[][] }; How can I get them? Sorry I'm a beginner :]
If you are iterating through your fileInformation list /array, then file in the context of the loop will be your information for that file. So if you needed to access the modTitle property, you could just use file[0] as it was the first value you set in the array you created. fileInformation is a string[][] but when iterating through the individual items using a foreach loop, you are just working with string[] items.
Mhh.. My program should show me 3 folders, but it only show one.. Maybe my variable for the function is not right? List<string[]> fileInformaton = fs.getModDesc(path)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.