-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoute Files to Printer.vb
83 lines (63 loc) · 3.18 KB
/
Route Files to Printer.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Imports System.Environment
Imports System.IO
Imports Windows.ApplicationModel.DataTransfer
Imports Windows.System.Profile
Namespace MyNamespace
Class MyClassVB
'\\DESKTOP-NAME\ZD220 Text Only
Shared PrinterShare As String
Shared Sub Main(args As String())
If args.Count = 0 Then
Console.WriteLine("Label Printer Share Name must be provided as command line argument.\n Exiting program.")
Else
PrinterShare = args(0)
Console.WriteLine("Label Printer: {0}", PrinterShare)
Dim userProfilePath As String = GetFolderPath(SpecialFolder.UserProfile)
Dim downloadsFolderPath As String = Path.Combine(userProfilePath, "Downloads")
Console.WriteLine("Watching folder " & downloadsFolderPath)
Using watcher = New FileSystemWatcher(downloadsFolderPath)
watcher.NotifyFilter = NotifyFilters.Attributes Or
NotifyFilters.CreationTime Or
NotifyFilters.DirectoryName Or
NotifyFilters.FileName Or
NotifyFilters.LastAccess Or
NotifyFilters.LastWrite Or
NotifyFilters.Security Or
NotifyFilters.Size
AddHandler watcher.Renamed, AddressOf OnRenamed
AddHandler watcher.Error, AddressOf OnError
watcher.Filter = "*(ZPL)*.txt"
watcher.IncludeSubdirectories = False
watcher.EnableRaisingEvents = True
Console.WriteLine("Press enter to exit.")
Console.ReadLine()
End Using
End If
End Sub
Private Shared Sub OnRenamed(sender As Object, e As RenamedEventArgs)
Console.WriteLine($"Printing label: {e.FullPath}")
Console.Write("to " & PrinterShare)
Dim filePath As String = e.FullPath
Dim StartInfo As New ProcessStartInfo("cmd.exe")
StartInfo.Arguments = "/c copy """ & e.FullPath & """ """ & PrinterShare & """ & DEL """ & e.FullPath & """"
Process.Start(StartInfo)
End Sub
Private Shared Sub OnError(sender As Object, e As ErrorEventArgs)
PrintException(e.GetException())
End Sub
Private Shared Sub PrintException(ex As Exception)
If ex IsNot Nothing Then
Console.WriteLine($"Message: {ex.Message}")
Console.WriteLine("Stacktrace:")
Console.WriteLine(ex.StackTrace)
Console.WriteLine()
PrintException(ex.InnerException)
End If
End Sub
Public Function GetDownloadsFolderPath() As String
Dim userProfilePath As String = GetFolderPath(SpecialFolder.UserProfile)
Dim downloadsFolderPath As String = Path.Combine(userProfilePath, "Downloads")
Return downloadsFolderPath
End Function
End Class
End Namespace