0

抱歉,如果之前有人问过,我发现其他解决方案对我来说太复杂了.. 无论如何,我正在尝试通过 cmd 在 Visual Basic 代码中搜索图像,并将图像路径保存为字符串,但我似乎无法捕获输出cmd 对。任何帮助将不胜感激,谢谢!

代码:

    Dim imageLocation As String
    Dim cmd As New Process
    Dim SR As System.IO.StreamReader
    cmd.StartInfo.FileName = "cmd.exe"
    cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    cmd.StartInfo.Arguments = "/C dir /b/s Roey.png"
    cmd.Start()
    SR = cmd.StandardOutput
    imageLocation = SR.ReadLine

更新所以我发现将输出保存到 txt 文件然后读取它可以更简单,所以我编写了以下代码:

        Dim cmd As New Process
        cmd.StartInfo.FileName = "cmd.exe"
        cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        cmd.StartInfo.Arguments = "/C dir /b/s Roey.png > 
        C:\Users\ירין\Desktop\Roeyyy\path.txt"
        cmd.Start()
        cmd.WaitForExit()

当我运行

    "dir /b/s Roey.png > 
    C:\Users\ירין\Desktop\Roeyyy\path.txt"

在 CMD 上它很完美,那么为什么它不能在这里工作呢?:(

4

2 回答 2

1

我发现了这个

Dim MyFilePath As String = Directory.GetFiles([SomePath], "*.png", SearchOption.AllDirectories).
     Where(Function(f) f.Contains("Roey.png")).FirstOrDefault()

解决了!

于 2018-10-07T21:18:29.170 回答
0

你是一个程序员,所以你搜索文件。

Imports System.Runtime.InteropServices
Sub Main
'On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")

ProcessFolder DirName

End Sub

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files

    For Each thing in Fls
         msgbox Thing.Name & " " & Thing.path 
         'fso.copyfile thing.path, "C:\backup"
    Next

    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        ProcessFolder thing.path
    Next

End Sub
于 2018-10-07T19:42:48.010 回答