2012-01-25 43 views
0

我有以下的代码,如果某些文件夹被创建,其检查;具有完整路径的文件夹列表存储在xml文件中。阅读来自XML元素和的方法中使用它们

namespace InstallationCheck 
{ 
    public class Checking 
    { 
     public bool result() 
     { 

      bool returns = true; 

     //Reads from xml file the element content from a tag line (ex: esecpath) 
      string esecpath = Checking.CitXml("C:\\testconfig.xml", "esecpath"); 
      string agentpath = Checking.CitXml("C:\\testconfig.xml", "agentpath"); 
      string datapath = Checking.CitXml("C:\\testconfig.xml", "datapath"); 
      string debugpath = Checking.CitXml("C:\\testconfig.xml", "debugpath"); 
      string helppath = Checking.CitXml("C:\\testconfig.xml", "helppath"); 
      string patchpath = Checking.CitXml("C:\\testconfig.xml", "patchpath"); 

      // Compare the paths from XML with the paths of the app. 
      List<bool> listtest = new List<bool>(); 
      listtest.Add((Directory.Exists(esecpath) == true)); 
      listtest.Add((Directory.Exists(agentpath) == true)); 
      listtest.Add((Directory.Exists(datapath) == true)); 
      listtest.Add((Directory.Exists(debugpath) == true)); 
      listtest.Add((Directory.Exists(patchpath) == true)); 
      listtest.Add((Directory.Exists(helppath) == true)); 

      //Cheking if any of paths are false 
      foreach (bool varia in listtest) 
      { 
       returns = returns && varia; 
      } 
      return returns; 
     } 

      // Reading from XML method 
     public static string CitXml(string xmlpath, string field) 
     { 
      XmlTextReader xmlReader = new XmlTextReader(xmlpath); 
      xmlReader.WhitespaceHandling = WhitespaceHandling.None; 

      xmlReader.ReadToDescendant(field); 
      return xmlReader.ReadElementString(field); 

     } 


    } 

} 

现在,我需要检查,如果某些文件被创建(其中许多人),饶了我manualy加入所有的代码,我不知道应该怎么做;我想让代码读取xml文件并检查是否所有文件(来自xml)都已创建。所以我想知道你是否有人可以给出一个ideea,一个提示(所以我可以去阅读它),也许是一个代码示例。谢谢。

+1

代码提示:不要写'== TRUE'。您可以安全,轻松地将所有这些与括号一起取出。 – Ryan

+0

请告诉我'Checking.CitXml()'不会加载整个XML文件并查找单个元素,然后返回它的值。而且你正在做这个,结束,结束...... 没关系。向下滚动失败。

回答

0

好了,看看是否有创建,重新编写代码是这样的:

public bool result() 
{  
    Dictionary<string, string> files = new Dictionary<string, string>(); 
    files.Add("esecpath", "C:\\testconfig.xml"); 
    // ... etc for each file 

    // if you want to see if any don't exist, then use ... 
    // if(files.Any(f => !File.Exists(f.Value))) 

    // else, these are all the created files 
    var createdFiles = files.Where(f => !File.Exists(f.Value)); 
    if(createdFiles.Count() > 0) 
    { 
     // A file doesn't exist! Therefore you are creating it! 
    } 
    var directories = files.Select(f => Checking.CitXml(f.Value, f.Key)); 

    return directories.All(d => Directory.Exists(d)); 
} 
+0

谢谢你(和其他人的提示),你能告诉我在这domanin是新的,我觉得我触碰冰山(称为C#)的尖端。我认为我形成了一个关于我该怎么做的理念。我忘了提及,如果另一个程序(安装)正在创建正确的文件夹/文件,我就会感到不安。 – lerys

+0

我在我的例子中使用了很多Linq,阅读/得到一本关于Linq的书,这对处理列表会有很大的帮助。 –

+0

如果这回答你的问题,用一个复选标记标记。或者如果你想要更多的答案,只需稍等一下别人就可以发布。 –