返回具有不同值

时间:2016-04-10 00:59:36

标签: c# arrays

如何返回多个具有不同值的数组?在我的第一个函数中,我得到了我文件夹中的所有文件。

在我的第二个函数中,我为每个文件提取" modDesc.xml"并从中获取我的信息。现在我想为每个文件返回一个包含所有这些信息的数组!但我不知道如何...我希望有人可以帮助我!

这是我的代码:

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 个答案:

答案 0 :(得分:1)

您可以返回一个List<string[]>(即一个数组列表),其中包含每个文件的数组集合:

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

或者,如果您的文件名都是唯一的,并且您希望更轻松地访问它们,则可以将它们存储在一个词典中,以便您可以通过它来查看每个文件的名称:

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

}

然后,如果您想从字典中的文件中访问信息,可以使用:

string[] information = yourDictionary["YourFileName"];