Guest User

WinappDebug

a guest
Feb 2nd, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.92 KB | None | 0 0
  1. Imports System.IO
  2. Module Module1
  3.  
  4.     Sub Main()
  5.  
  6.         'Check if the winapp2.ini file is in the current directory. End program if it isn't.
  7.         If File.Exists(Environment.CurrentDirectory & "\winapp2.ini") = False Then
  8.             Console.WriteLine("winapp2.ini file could not be located in the current working directory (" & Environment.CurrentDirectory & ")")
  9.             Console.ReadKey()
  10.             End
  11.         End If
  12.  
  13.         'Create some variables for later use
  14.         Dim linecount As Integer = 0
  15.         Dim command As String
  16.         Dim number_of_errors As Integer = 0
  17.  
  18.         'Create a list of supported environmental variables
  19.         Dim envir_vars As New List(Of String)
  20.         envir_vars.AddRange(New String() {"%userprofile%", "%ProgramFiles%", "%rootdir%", "%windir%", "%appdata%", "%systemdrive%"})
  21.  
  22.  
  23.         'Read the winapp2.ini file line-by-line
  24.         Dim r As IO.StreamReader
  25.         Try
  26.  
  27.             r = New IO.StreamReader(Environment.CurrentDirectory & "\winapp2.ini")
  28.             Do While (r.Peek() > -1)
  29.  
  30.                 'Update the line that is being tested
  31.                 command = (r.ReadLine.ToString)
  32.  
  33.                 'Increment the lineocunt value
  34.                 linecount = linecount + 1
  35.  
  36.                 'Whitespace checks
  37.                 If command = "" = False And command.StartsWith(";") = False Then
  38.  
  39.                     'Check for trailing whitespace
  40.                     If command.EndsWith(" ") Then
  41.                         Console.WriteLine("Line: " & linecount & " Error: Detected unwanted whitespace at end of line." & Environment.NewLine & "Command: " & command & Environment.NewLine)
  42.                         number_of_errors = number_of_errors + 1
  43.                     End If
  44.  
  45.                     'Check for ending whitespace
  46.                     If command.StartsWith(" ") Then
  47.                         Console.WriteLine("Line: " & linecount & " Error: Detected unwanted whitespace at beginning of line." & Environment.NewLine & "Command: " & command & Environment.NewLine)
  48.                         number_of_errors = number_of_errors + 1
  49.                     End If
  50.  
  51.                 End If
  52.  
  53.                 'Check for spelling errors in "LangSecRef"
  54.                 If command.StartsWith("Lan") Then
  55.                     If command.Contains("LangSecRef=") = False Then
  56.                         Console.WriteLine("Line: " & linecount & " Error: 'LangSecRef' entry is incorrectly spelled or formatted." & Environment.NewLine & "Command: " & command & Environment.NewLine)
  57.                         number_of_errors = number_of_errors + 1
  58.                     End If
  59.                 End If
  60.  
  61.                 If command = "Default=True" Or command = "default=true" Then
  62.                     Console.WriteLine("Line: " & linecount & " Error: All entries should be disabled by default." & Environment.NewLine & "Command: " & command & Environment.NewLine)
  63.                     number_of_errors = number_of_errors + 1
  64.                 End If
  65.  
  66.                 'Check for environmental variable spacing errors
  67.                 If command.Contains("%Program Files%") Then
  68.                     Console.WriteLine("Line: " & linecount & " Error: '%ProgramFiles%' variable should not have spacing." & Environment.NewLine & "Command: " & command & Environment.NewLine)
  69.                     number_of_errors = number_of_errors + 1
  70.                 End If
  71.  
  72.                 'Check for cleaning command spelling errors (files)
  73.                 If command.StartsWith("Fi") Then
  74.                     If command.Contains("FileKey") = False Then
  75.                         Console.WriteLine("Line: " & linecount & " Error: 'FileKey' entry is incorrectly spelled or formatted. Spelling should be CamelCase." & Environment.NewLine & "Command: " & command & Environment.NewLine)
  76.                         number_of_errors = number_of_errors + 1
  77.                     End If
  78.                 End If
  79.  
  80.                 'Check for cleaning command spelling errors (registry keys)
  81.                 If command.StartsWith("Re") Then
  82.                     If command.Contains("RegKey") = False Then
  83.                         Console.WriteLine("Line: " & linecount & " Error: 'RegKey' entry is incorrectly spelled or formatted. Spelling should be CamelCase." & Environment.NewLine & "Command: " & command & Environment.NewLine)
  84.                         number_of_errors = number_of_errors + 1
  85.                     End If
  86.                 End If
  87.  
  88.                 'Check for missing numbers next to cleaning commands
  89.                 If command.StartsWith("FileKey=") Or command.StartsWith("RegKey=") Then
  90.                     Console.WriteLine("Line: " & linecount & " Error: Cleaning path entry needs to have a trailing number." & Environment.NewLine & "Command: " & command & Environment.NewLine)
  91.                     number_of_errors = number_of_errors + 1
  92.                 End If
  93.  
  94.                 'Check for Detect that contains a filepath instead of a registry path
  95.                 If command.StartsWith("Detect=%") Or command.StartsWith("Detect=C:\") Then
  96.                     Console.WriteLine("Line: " & linecount & " Error: 'Detect' can only be used for registry key paths." & Environment.NewLine & "Command: " & command & Environment.NewLine)
  97.                     number_of_errors = number_of_errors + 1
  98.                 End If
  99.  
  100.                 'Check for detectfile that contains a registry path
  101.                 If command.StartsWith("DetectFile=HKLM") Or command.StartsWith("DetectFile=HKCU") Or command.StartsWith("DetectFile=HKC") Or command.StartsWith("DetectFile=HKCR") Then
  102.                     Console.WriteLine("Line: " & linecount & " Error: 'DetectFile' can only be used for filesystem paths." & Environment.NewLine & "Command: " & command & Environment.NewLine)
  103.                     number_of_errors = number_of_errors + 1
  104.                 End If
  105.  
  106.                 'Check for missing backslashes on environmental variables
  107.                 For Each var As String In envir_vars
  108.                     If command.Contains(var) = True Then
  109.                         If command.Contains(var & "\") = False Then
  110.                             Console.WriteLine("Line: " & linecount & " Error: Environmental variables must have a trailing backslash." & Environment.NewLine & "Command: " & command & Environment.NewLine)
  111.                             number_of_errors = number_of_errors + 1
  112.                         End If
  113.                     End If
  114.                 Next
  115.  
  116.  
  117.             Loop
  118.  
  119.             'Close unmanaged code calls
  120.             r.Close()
  121.  
  122.  
  123.                 Catch ex As Exception
  124.             Console.WriteLine(ex.Message)
  125.         End Try
  126.  
  127.  
  128.  
  129.         'Stop the program from closing on completion
  130.         Console.WriteLine("***********************************************" & Environment.NewLine & "Completed analysis of winapp2.ini. " & number_of_errors & " errors were detected. Press any key to close.")
  131.         Console.ReadKey()
  132.  
  133.  
  134.     End Sub
  135.  
  136. End Module
Advertisement
Add Comment
Please, Sign In to add comment