-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteLogReader.cs
136 lines (120 loc) · 4.82 KB
/
RemoteLogReader.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace agentspy.net
{
class RemoteLogReader : ILogReader
{
[DllImport("Mpr.dll")]
private static extern int WNetUseConnection(
IntPtr hwndOwner,
NETRESOURCE lpNetResource,
string lpPassword,
string lpUserID,
int dwFlags,
string lpAccessName,
string lpBufferSize,
string lpResult
);
[DllImport("Mpr.dll")]
private static extern int WNetCancelConnection2(string lpName, int dwFlags, bool fForce);
[StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
public ResourceScope dwScope = 0;
public ResourceType dwType = 0;
public ResourceDisplayType dwDisplayType = 0;
public ResourceUsage dwUsage = 0;
public string lpLocalName = null;
public string lpRemoteName = null;
public string lpComment = null;
public string lpProvider = null;
};
public enum ResourceScope
{
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET,
RESOURCE_REMEMBERED,
RESOURCE_RECENT,
RESOURCE_CONTEXT
};
public enum ResourceType
{
RESOURCETYPE_ANY,
RESOURCETYPE_DISK,
RESOURCETYPE_PRINT,
RESOURCETYPE_RESERVED
};
public enum ResourceUsage
{
RESOURCEUSAGE_CONNECTABLE = 0x00000001,
RESOURCEUSAGE_CONTAINER = 0x00000002,
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
RESOURCEUSAGE_SIBLING = 0x00000008,
RESOURCEUSAGE_ATTACHED = 0x00000010,
RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
};
public enum ResourceDisplayType
{
RESOURCEDISPLAYTYPE_GENERIC,
RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_SERVER,
RESOURCEDISPLAYTYPE_SHARE,
RESOURCEDISPLAYTYPE_FILE,
RESOURCEDISPLAYTYPE_GROUP,
RESOURCEDISPLAYTYPE_NETWORK,
RESOURCEDISPLAYTYPE_ROOT,
RESOURCEDISPLAYTYPE_SHAREADMIN,
RESOURCEDISPLAYTYPE_DIRECTORY,
RESOURCEDISPLAYTYPE_TREE,
RESOURCEDISPLAYTYPE_NDSCONTAINER
};
const int CONNECT_INTERACTIVE = 0x00000008;
const int CONNECT_PROMPT = 0x00000010;
const int CONNECT_REDIRECT = 0x00000080;
const int CONNECT_UPDATE_PROFILE = 0x00000001;
const int CONNECT_COMMANDLINE = 0x00000800;
const int CONNECT_CMD_SAVECRED = 0x00001000;
const int CONNECT_LOCALDRIVE = 0x00000100;
const string LOGPATH = @"c$\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE";
const string AgentLog = "VSTTAgentProcess.log";
private const string UserName = "tfsbuild3_test";
private const string Password = "adm$pwd$4$SystemInt";
private readonly string mySharedFolder;
private readonly StreamReader myStreamReader;
public RemoteLogReader(string computer)
{
this.mySharedFolder = Path.Combine(@"\\" + computer, LOGPATH);
var nr = new NETRESOURCE
{
dwType = ResourceType.RESOURCETYPE_DISK,
lpLocalName = null,
lpRemoteName = this.mySharedFolder,
lpProvider = null
};
int result = WNetUseConnection(IntPtr.Zero, nr, Password, UserName, 0, null, null, null);
//int result = WNetUseConnection(IntPtr.Zero, nr, "", "", CONNECT_INTERACTIVE | CONNECT_PROMPT, null, null, null);
if (result != 0)
{
Console.WriteLine("Error {0} at connecting.", result);
return;
}
var fileStream = new FileStream(Path.Combine(this.mySharedFolder, AgentLog), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
this.myStreamReader = new StreamReader(fileStream);
}
public string ReadLine()
{
return this.myStreamReader != null ? this.myStreamReader.ReadLine() : string.Empty;
}
public bool EndOfStream { get { return this.myStreamReader == null || this.myStreamReader.EndOfStream; } }
public void Dispose()
{
if (myStreamReader != null ) myStreamReader.Dispose();
var ret = WNetCancelConnection2(mySharedFolder, CONNECT_UPDATE_PROFILE, true);
if (0 != ret)
{
Console.WriteLine("return value at closing the connection {0}", ret);
}
}
}
}