2017-09-26 48 views
-2

希望你的淵博知識的基礎可以幫助在這裏,文件搜索在PC名VB6

即時通訊尋求建立一個搜索和開放代碼,

我要搜索的計算機名(有位實現)並使用該名稱搜索文件夾中的文本文件。該文件將包含一行數據,我想用它來配置我的系統註銷的頁面,如果文件不存在,我想打開一個名爲default的文件並使用其中的數據行。 即。即時通訊卡上的位是能夠檢查文件夾的擴展名將相同但名稱可以從PC更改爲PC的文件。 原因是我需要驗證和測試編程,如果我更改代碼內的註銷頁面數據,每次需要對其進行更改時都需要對其進行測試。通過這種方式更新每個帶有合適的註銷數據的pc命名的文本文件列表,這些都是需要更改的。

有足夠的資源爲VB.NEt努力尋找VB6的東西。

PS。在這非常綠色。

感謝 審查附帶的代碼,如果任何人有足夠的時間,

再次感謝

Type Point_Type 
x As Long 
y As Long 
End Type 

'Allows user to set logout duration 
'Logout Duration = timer interval x 19 
'Change timer interval or max LogoutCheckCounter to change duration 
Dim LogoutCheckCounter As Integer 

Declare Function GetCursorPos Lib "user32.dll" (lpPoint As Point_Type) As 
Long 


Private Declare Function apiGetComputerName Lib "kernel32" Alias _ 
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long 


Public Sub InacLogOut() 

On Error GoTo Err_ 

Dim CheckUser As String 
Dim coord As Point_Type 
Dim retval As Long 
Dim xPos As Long 
Dim yPos As Long 
Dim line As Integer 
Dim page As integer 
Dim LogoutTime As String 
Dim LogoutPage As String 
Dim MyComputerName As String 
Static xOldPos As Long 


'get current cursor position 
retval = GetCursorPos(coord) 
xPos = coord.x 
yPos = coord.y 

'code executes every 30 seconds 

'If mouse is not moved the old position nwill equal t r log in default user 
If xOldPos = xPos Then 

'get the current user 
CheckUser = System.LoginUserName 

If CheckUser = "NOBODY" Then 
    'User is already logged out, exit sub without action 
    Exit Sub 

Else  
    'get logout time 
    line = FreeFile 
    If Dir("C:\LogoutTime.txt") <>"" Then 
     Open "C:\LogoutTime.txt" For Input As line 
     Line Input #line, LogoutTime 
     MsgBox (LogoutTime) 
     Close File 
    Else 
    LogoutTime = 10 
    End if 

    ' get logout page 
    MyComputerName = fOSMachineName 

    Dim FiletoOpen as string 
FiletoOpen = "C:\" & MyComputerName & ".txt" 

    If Dir(FiletoOpen) <>"" Then 
     Open "FiletoOpen" For Input As page 
     Line Input #page, LogoutPage 
     MsgBox (LogoutPage) 
     Close File 
    Else 
    LogoutPage = MainPage 
    End if 

     'code executes every 30 seconds, LogoutCheckCounter used to set the 
    logout interval 
    If LogoutCheckCounter > LogoutTime Then 
    ' code to kill report tool if open 
     Shell "taskkill.exe /f /t /im ReportGenerator.exe" 
     msgbox (Kill task) 

     User.ReplaceCentralFrame LogoutPage 
     System.FixLogin "NOBODY", "" 

     Else 
     LogoutCheckCounter = LogoutCheckCounter + 1 
     End If 
    End If 

Else 
    'mouse has been moved, reset LogoutCheckCounter 
    LogoutCheckCounter = 0 

End If 

'xOldPos will initially be zero the first time the code runs 
xOldPos = xPos 
Err_: 

End Sub 
'----------------------------------------------- 
Function fOSMachineName() As String 
Dim lngLen As Long, lngX As Long 
Dim strCompName As String 
lngLen = 16 
strCompName = String$(lngLen, 0) 
lngX = apiGetComputerName(strCompName, lngLen) 
If lngX <> 0 Then 
fOSMachineName = Left$(strCompName, lngLen) 
Else 
fOSMachineName = "" 
End If 
End Function 

'---------------------------------------------------` 
+0

你也要打個咖啡嗎?你在問一些不可能的事情。 –

+0

沒有什麼是不可能的;只是很難。我有讀取PC名稱的代碼並操作包含以PC命名的文本文件的文件夾。我只需要一種方法來加入找到的文件名(PC名稱)和已知的擴展名。例如,在PC-001001上,我使用PC名稱在C:\ LogoutCode \中有一個文件。 PC001001.txt我只需要一種方法來加入字符串的路徑......「C:\ LogoutCode \」&&「PC001001.txt」,並感謝您的快速響應 –

+0

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/operators-and-expressions/concatenation-operators – Marc

回答

0

它在VB6比馬克引用做得有點不同。您不要在VB6中的同一行上聲明和初始化變量。繼微軟的例子:

Dim x As String 
Dim y As String 

x = "Mic" & "ro" & "soft" 
y = "Mic" + "ro" + "soft" 

Dim a As String 
Dim d As String 
Dim z As String 
Dim w As String 

a = "abc" 
d = "def" 
z = a & d 
w = a + d 

的「&」運營商可以使用任何數據類型和變量將被轉換成字符串。 「+」運算符只適用於字符串,如果任何變量或文字不是字符串,將會引發編譯或運行時錯誤。

Dim a as string 
Dim x as long 

x = 123 
a = "abc" & x 
results in "abc123" 

a = "abc" + x 
results in an error 

所以

Dim strPath as String 
Dim strName as String 
Dim strFileSpec as String 

strPath = "C:\LogoutCode\" 
strName = "PC001001" 
strFileSpec = strPath + strName + ".txt" 
+0

thanks @ thx1138v2 –