Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Word break tag : <wbr/> (HTML5) The  HTML  <wbr>  tag  is  used  defines  a  potential  line  break  point  if  needed.  This  stands  for  Word  BReak. This  is  u... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
Test polymorphism in reflection method invoke
package y11.m04;

import java.lang.reflect.Method;

public class d28t01 {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // Test polymorphism in reflection
        Object invoker = new Invoker();
        Class clazz = invoker.getClass();

        Dialer p1 = new Phone("12345");
        Phone p2 = new Phone("234567");
        CellPhone p3 = new CellPhone("345678");

        // cause java.lang.NosuchMethodException
        try {
            Method m1 = clazz.getDeclaredMethod("use", p1.getClass());
            m1.invoke(invoker, p1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Method m2 = clazz.getDeclaredMethod("use", p2.getClass());
            m2.invoke(invoker, p1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Method m3 = clazz.getDeclaredMethod("use", p3.getClass());
            m3.invoke(invoker, p1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Method[] ms = clazz.getDeclaredMethods();
        for (Method m : ms) {
            if (m.getName().equals("use")) {
                Class[] paramTypes = m.getParameterTypes();
                if (paramTypes.length == 1 && paramTypes[0].isAssignableFrom(p1.getClass())) {
                    m.invoke(invoker, p1);
                }
            }

            if (m.getName().equals("use")) {
                Class[] paramTypes = m.getParameterTypes();
                if (paramTypes.length == 1 && paramTypes[0].isAssignableFrom(p2.getClass())) {
                    m.invoke(invoker, p2);
                }
            }

            if (m.getName().equals("use")) {
                Class[] paramTypes = m.getParameterTypes();
                if (paramTypes.length == 1 && paramTypes[0].isAssignableFrom(p3.getClass())) {
                    m.invoke(invoker, p3);
                }
            }
        }
    }
}

class Invoker {
    public void use(Dialer dialer) {
        if (dialer != null)
            dialer.call();
    }
}

interface Dialer {
    void call();
}

class Phone implements Dialer {
    public String number;

    public Phone(String number) {
        this.number = number;
    }

    public void call() {
        System.out.printf("Call %s%n", number);
    }
}

class CellPhone extends Phone {

    public CellPhone(String number) {
        super(number);
    }

    @Override
    public void call() {
        System.out.printf("Dial %s%n", number);
    }
}
Share folder info (netapi32)
class MainConsole
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    struct SHARE_INFO_2
    {
        [MarshalAs(UnmanagedType.LPWStr)]
        public string shi2_netname;
        public uint shi2_type;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string shi2_remark;
        public uint shi2_permissions;
        public uint shi2_max_uses;
        public uint shi2_current_uses;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string shi2_path;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string shi2_passwd;
    }

    static string FormatMessage(int errCode)
    {
        switch (errCode)
        {
            case ERROR_ACCESS_DENIED: return "The user does not have access to the requested information.";
            case ERROR_INVALID_LEVEL: return "The value specified for the level parameter is invalid.";
            case ERROR_INVALID_PARAMETER: return "The specified parameter is invalid.";
            case ERROR_MORE_DATA: return "More entries are available. Specify a large enough buffer to receive all entries.";
            case ERROR_NOT_ENOUGH_MEMORY: return "Insufficient memory is available.";
            case NERR_BufTooSmall: return "The supplied buffer is too small.";
            case NERR_NetNameNotFound: return "The share name does not exist.";
        };

        return null;
    }

    [DllImport("Netapi32", CharSet = CharSet.Auto)]
    static extern int NetApiBufferFree(IntPtr Buffer);

    [DllImport("Netapi32", CharSet = CharSet.Auto)]
    static extern int NetShareGetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string netname, int level, ref IntPtr bufptr);

    /// <summary>
    /// Retrieves the local path for the given server and share name.
    /// </summary>
    /// <remarks>serverName must start with \\</remarks>
    static string NetShareGetPath(string serverName, string netName)
    {
        string path = null;
        IntPtr ptr = IntPtr.Zero;

        int errCode = NetShareGetInfo(serverName, netName, 2, ref ptr);
        if (errCode == NERR_Success)
        {
            SHARE_INFO_2 shareInfo = (SHARE_INFO_2)Marshal.PtrToStructure(ptr, typeof(SHARE_INFO_2));

            var members = from m in shareInfo.GetType().GetFields()
                          select m;

            foreach (var m in members)
            {
                Console.WriteLine("{0}={1}", m.Name, m.GetValue(shareInfo));
            }


            path = shareInfo.shi2_path;
            NetApiBufferFree(ptr);
        }
        else
            Console.WriteLine(FormatMessage(errCode));

        return path;
    }

    /// <summary>
    /// The Main method is the entry point of the program, where the program control starts and
    /// ends.
    /// </summary>
    /// <param name="args"></param>
    /// <returns></returns>
    [STAThread]
    static int Main(string[] args)
    {
        Console.WriteLine("path=" + NetShareGetPath(@"\\s3t21", "檔案測試區"));

        Console.Read();
        return 0;
    }

    const int ERROR_ACCESS_DENIED = 5;
    const int ERROR_INVALID_LEVEL = 124; // unimplemented level for info
    const int ERROR_INVALID_PARAMETER = 87;
    const int ERROR_MORE_DATA = 234;
    const int ERROR_NOT_ENOUGH_MEMORY = 8;
    const int NERR_BufTooSmall = 2123; // The API return buffer is too small.
    const int NERR_NetNameNotFound = 2310; // This shared resource does not exist.
    const int NERR_Success = 0;

} // class MainConsole
http://pinvoke.net/default.aspx/netapi32.NetShareGetInfo
Get share folders from wmi
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from win32_share");
foreach (ManagementObject share in searcher.Get())
{
    Console.WriteLine("==============");
    ManagementBaseObject baseObj = share as ManagementBaseObject;

    PropertyDataCollection.PropertyDataEnumerator en = share.Properties.GetEnumerator();
    while (en.MoveNext())
    {
        Console.WriteLine(en.Current.Name + "=" + en.Current.Value);
    }
}
Web based file manager
//KeyNumber : 102(int)
//ProductName : ad(string)
//ProductVersion : ad1.0.2.1011010(string)
//LicenseCount : 9999(int)
//UserCount : 9999(int)
Cryptor cryptor = new Cryptor();
string key = HexEncoding.ToString(cryptor.EncryptString("102:ad:ad1.0.2.1011010:9999:9999"));
Tick
Start tick
Java Javascript .net
Tick = 0 (GMT+0) 1970/01/01 00:00:00 1970/01/01 00:00:00 1601/01/01 00:00:00
sec/tick 1000 1000 10000000
Convertion
tick=0\to tick Java Javascript .net
Java x 0 621356256000000000
Javascript 0 x 621356256000000000
.net -11644473600000 -11644473600000 x
// Convert c# date to javascript
var ticks = (DateTime.Now.Ticks - 621356256000000000) / 10000;
// Convert javascript date to c#
var ticks = (((new Date()).getTime() * 10000) + 621355968000000000);