/[AnywhereTS-MSSQL]/trunk/TSAdminTool/UNCconverter.cs
ViewVC logotype

Contents of /trunk/TSAdminTool/UNCconverter.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 64 - (show annotations) (download)
Thu Jul 12 18:56:31 2012 UTC (7 years, 7 months ago) by william
File size: 2434 byte(s)
+ more exception logging

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Runtime.InteropServices;
5 using System.ComponentModel;
6
7
8 namespace AnywhereTS
9 {
10 static class UNCconverter
11 {
12 [DllImport("mpr.dll")]
13 public static extern int WNetGetUniversalName(
14 string lpLocalPath,
15 int dwInfoLevel,
16 ref StringBuilder lpBuffer,
17 ref int lpBufferSize);
18
19
20 const int UNIVERSAL_NAME_INFO_LEVEL = 1;
21 const int ERROR_MORE_DATA = 234;
22 // optional:
23 const int ERROR_NOT_CONNECTED = 2250;
24 const int MAX_PATH = 260;
25
26
27 public static string ConvertLocalPathToUnc(
28 string localPath)
29 {
30 StringBuilder buffer = new StringBuilder();
31 int size = 0;
32 // First call WNetGetUniversalName with an
33 // empty buffer to get size of buffer needed.
34 int retVal = WNetGetUniversalName(
35 localPath, UNIVERSAL_NAME_INFO_LEVEL,
36 ref buffer, ref size);
37 if (retVal == ERROR_MORE_DATA)
38 {
39 // WNetGetUniversalName returns the space
40 // needed in size, now allocate space for a
41 // buffer of this size.
42 buffer = new StringBuilder(size);
43 retVal = WNetGetUniversalName(
44 localPath, UNIVERSAL_NAME_INFO_LEVEL,
45 ref buffer, ref size);
46 if (retVal != 0)
47 {
48 Win32Exception ex = new Win32Exception(retVal);
49 using (log4net.NDC.Push(string.Format("Win32Exception: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
50 {
51 Logging.ATSAdminLog.Error(string.Format("Failed to convert LOCAL Path: {0} to a UNC Path", localPath));
52 }
53 return "";
54 }
55 }
56 else
57 {
58 Win32Exception ex = new Win32Exception(retVal);
59 using (log4net.NDC.Push(string.Format("Win32Exception: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
60 {
61 Logging.ATSAdminLog.Error(string.Format("Failed to convert LOCAL Path: {0} to a UNC Path", localPath));
62 }
63 return "";
64 }
65 return buffer.ToString();
66 }
67 }
68 }

  ViewVC Help
Powered by ViewVC 1.1.22