Are optional params available for the Compact Framework?

大城市里の小女人 提交于 2019-12-24 12:01:11

问题


I need to get a list of all files on a handheld device whose names fit a certain pattern, such as "ABC.XML"

I adapted code from here (Hernaldo's answer), like so:

public static List<string> GetXMLFiles(string fileType, string dir)
{
    string dirName = dir; // call it like so: GetXMLFiles("ABC", "\\");  <= I think the double-whack is what I need for Windows CE device...am I right?
    var fileNames = new List<String>();
    try
    {
        foreach (string f in Directory.GetFiles(dirName))
        {
            if ((f.Contains(fileType)) &&  (f.Contains(".XML")))
            {
                fileNames.Add(f);
            }
        }
        foreach (string d in Directory.GetDirectories(dirName))
        {
            GetXMLFiles(fileType, d);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    return fileNames;
}

...but each time the method recursively calls itself (in the GetDirectories() loop), I'm passing the same old first arg. Is it possible (in Compact Framework) to do something like this instead:

public static List<string> GetXMLFiles(optional string fileType, string dir)
{
        . . .        
        foreach (string d in Directory.GetDirectories(dirName))
        {
            GetXMLFiles(dir = d);
        }
        . . .

?

UPDATE

According to Habib, this should work (new "try" section):

try
{
    string filePattern = string.Format("*{0}*.XML", fileType);
    foreach (string f in Directory.GetFiles(dirName, filePattern))
    {
        fileNames.Add(f);
    }
    foreach (string d in Directory.GetDirectories(dirName))
    {
        GetXMLFiles(fileType, d);
    }
}

Ja?

UPDATE 2

This goes along with my second response to Alan's comment below:

const string EXTENSION = ".XML";
. . .
try
{
    foreach (string f in Directory.GetFiles(dirName))
    {
        string ext = Path.GetExtension(f);
        string fileNameOnly = Path.GetFileNameWithoutExtension(f);
        if ((ext.Equals(EXTENSION, StringComparison.Ordinal)) && (fileNameOnly.Contains(fileType)))
        {
            fileNames.Add(f);
        }
    }
    foreach (string d in Directory.GetDirectories(dirName))
    {
        GetXMLFiles(fileType, d);
    }
}

回答1:


Optional parameters are a feature of .NET 4.0, so you won't be able to use them. The typical solution to this with a recursive function is to create two overloads for your method with different parameters.

For example,

BinarySearch(array a);
{
  BinarySearch(a, -1, array.Length);
} 

BinarySearch(array, low, high)
{
   //code to update low high
   return BinarySearch(array, low, high);
}

In this case, the method which kicks off the recursion has a slightly different signature. You could do the same.




回答2:


Directory.GetFiles has an overload which takes search pattern you can use that:

Directory.GetFiles(dirName, "ABC.XML")

You can also use wildcards like "*.XML" which would return all XML files.



来源:https://stackoverflow.com/questions/22178321/are-optional-params-available-for-the-compact-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!