这个vb.net代码(几行用来递归搜索文件的代码)翻译成c#报错,要怎么改呢?
菜鸟,求帮忙.几行用来递归搜索文件的代码. 多谢!!
机器自动翻译后的c#(报错的地方见注释部分):
vb.net原码:
机器自动翻译后的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
...全文
相关推荐
.net 递归算法 .net 递归算法.net 递归算法
- **理解问题与分解**:正确理解和分解问题是成功实现递归的关键,必须确保每次递归都将问题简化到更基本的形式。在.NET中,递归算法可以通过C#、VB.NET或其他.NET语言实现。
中英文转换vb.net源代码
这是一个控件类,有DLL文件,直接拖到你的winform理就可以用了。不管你是用的vb.net, C#,还是C++,都可以直接调用。它会自见access数据库,用了存放控件名称,控件名是在程序中递归读
C#/ VB.Net中的高级WPF TreeViews第3部分,共n部分
总结来说,本教程系列的第三部分将深入讲解如何在C#和VB.Net的WPF应用中高效地管理和搜索TreeViews,包括数据绑定、动态过滤、节点操作等核心技巧。
.net算法 递归
基本情况是最简单的情况,可以直接得出答案;而递归情况则是将问题分解为更小的子问题,直到达到基本情况为止。在.NET中,我们可以使用C#、VB.NET等语言实现递归。
ASP.NET递归生成树导航示例
开发者可能使用C#或VB.NET作为后端语言,并利用ASP.NET的控件,如TreeView,来呈现树形视图。TreeView控件是ASP.NET中用于显示层次结构数据的可视化组件。


