读书人

这个vb.net代码(几行用来递归搜索文件

发布时间: 2012-09-12 09:21:30 作者: rapoo

这个vb.net代码(几行用来递归搜索文件的代码)翻译成c#报错,要怎么改呢?
菜鸟,求帮忙.几行用来递归搜索文件的代码. 多谢!!
机器自动翻译后的c#(报错的地方见注释部分):

C# code
    private static string[] FoundFile;private static long Ntx;public static string[] SearchFileInPath(string thePath, string theFileName, bool mStop = false)//--------------这里 mStop As Boolean = False 报错    {        if (Strings.Right(thePath, 1) != "\\")            thePath = thePath + "\\";        GetFileLoop(thePath, theFileName, mStop);        return FoundFile;    }    private static string GetFileLoop(string CurrentPath, string SearFile, bool mStop = false)//-------这里 mStop As Boolean = False 也报错    {        string functionReturnValue = null;        int nI = 0;        int nDirectory = 0;        long i = 0;        string sFileName = null;        string[] sDirectoryList = null;        sDirectoryList = new string[-1 + 1];                 // ERROR: Not supported in C#: OnErrorStatement        sFileName = FileSystem.Dir(CurrentPath, Constants.vbHidden | Constants.vbDirectory | Constants.vbReadOnly | Constants.vbSystem);//--------------FileSystem.Dir在c#中是什么函数呢?Constants.也报错        while (!string.IsNullOrEmpty(sFileName)) {            if (Strings.UCase(sFileName) // ERROR: Unknown binary operator Like//------------这里问题大了,要怎么改呢) {                i = FileSystem.GetAttr(CurrentPath + sFileName);                if ((i & Constants.vbDirectory) == 0) {                    if (mStop == false) {                        Array.Resize(ref FoundFile, Ntx + 1);                        FoundFile[Ntx] = CurrentPath + sFileName;                        Ntx = Ntx + 1;                    //Application.DoEvents()                    //Sleep(5)                    } else {                        functionReturnValue = CurrentPath + sFileName;                        return functionReturnValue;                    }                }            }            if (sFileName != "." & sFileName != "..") {                if (FileSystem.GetAttr(CurrentPath + sFileName) & Constants.vbDirectory) {                    nDirectory = nDirectory + 1;                    Array.Resize(ref sDirectoryList, nDirectory + 1);//--------错误    283    与“System.Array.Resize<string>(ref string[], int)”最匹配的重载方法具有一些无效参数,要怎么改呢?                    sDirectoryList[nDirectory] = CurrentPath + sFileName;                }            }            sFileName = FileSystem.Dir();        }        for (nI = 1; nI <= nDirectory; nI++) {            functionReturnValue = GetFileLoop(sDirectoryList[nI] + "\\", SearFile);            if (!string.IsNullOrEmpty(functionReturnValue) & mStop == true)                break; // TODO: might not be correct. Was : Exit For//这里应该是exit for吧?        }        return functionReturnValue;    }


vb.net原码:
VB code
 Private FoundFile() As String '存放传回值的字串阵列 Private Ntx As Long    Public Function SearchFileInPath(ByVal thePath As String, ByVal theFileName As String, Optional ByVal mStop As Boolean = False) As String()//-------这里 mStop As Boolean = False 也报错        If Strings.Right(thePath, 1) <> "\" Then thePath = thePath & "\"        Call GetFileLoop(thePath, theFileName, mStop)        SearchFileInPath = FoundFile    End Function    Private Function GetFileLoop(ByVal CurrentPath As String, ByVal SearFile As String, Optional ByVal mStop As Boolean = False) As String//-------这里 mStop As Boolean = False 也报错        Dim nI As Integer, nDirectory As Integer, i As Long        Dim sFileName As String        Dim sDirectoryList() As String        ReDim sDirectoryList(-1)        ' Ntx = 0        On Error Resume Next        sFileName = Dir(CurrentPath, vbHidden Or vbDirectory Or vbReadOnly Or vbSystem)        Do While sFileName <> ""            If UCase(sFileName) Like UCase(SearFile) Then                i = GetAttr(CurrentPath + sFileName)                If (i And vbDirectory) = 0 Then                    If mStop = False Then                        ReDim Preserve FoundFile(Ntx)                        FoundFile(Ntx) = CurrentPath + sFileName                        Ntx = Ntx + 1                        Application.DoEvents()                        Sleep(5)                    Else                        GetFileLoop = CurrentPath + sFileName                        Exit Function                    End If                End If            End If            If sFileName <> "." And sFileName <> ".." Then                If GetAttr(CurrentPath & sFileName) _                And vbDirectory Then                    nDirectory = nDirectory + 1                    ReDim Preserve sDirectoryList(nDirectory)                    sDirectoryList(nDirectory) = CurrentPath & sFileName                End If            End If            sFileName = Dir()        Loop        For nI = 1 To nDirectory            GetFileLoop = GetFileLoop(sDirectoryList(nI) & "\", SearFile)            If GetFileLoop <> "" And mStop = True Then Exit For        Next nI    End Function 



[解决办法]
不需要这么麻烦,
System.IO.Directory.GetFiles(
string path,
string searchPattern,
SearchOption searchOption /* AllDirectories 为递归子文件夹 */
);

http://msdn.microsoft.com/zh-cn/library/ms143448.aspx

读书人网 >C#

热点推荐