这个vb.net代码(几行用来递归搜索文件的代码)翻译成c#报错,要怎么改呢?

wnujy611861 2012-09-01 07:15:59
菜鸟,求帮忙.几行用来递归搜索文件的代码. 多谢!!
机器自动翻译后的c#(报错的地方见注释部分):
	
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原码:

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
...全文
116 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wnujy611861 2012-09-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

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

http://msdn.microsoft.com/zh-cn/library/ms143448.aspx
[/Quote]谢谢!它这个有没有权限限制什么的?就是如果文件权限限制了会不会不返回任何结果什么的?
devmiao 2012-09-01
  • 打赏
  • 举报
回复
不需要这么麻烦,
System.IO.Directory.GetFiles(
string path,
string searchPattern,
SearchOption searchOption /* AllDirectories 为递归子文件夹 */
);

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

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧