Announcement

Collapse
No announcement yet.

List network computers/shared folders

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • List network computers/shared folders

    good evening, i was wondering if you could help me with this...

    I need to list all the computers in a LAN and their shared forders so that you can click and choose the location (this is not the location where you want to install the software).

    I need this to map a unit only if certain folder exist in the server.

    Can you give me a hand?

    Thanks in advance

    Nick

  • #2
    Hi

    I call a c# executable that I wrote that builds an xml file containing all the network computers and the ip address of each nic installed on that computer. I call this using File.Run and then parse out the xml file created to extract what I need. You could modify this to get shared folders in addition to or instead of ip address's.

    Here is the source:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Net;
    using System.DirectoryServices;
    using System.IO;

    namespace TT_NetworkComputers
    {
    class Program
    {
    //declare the Netapi32 : NetServerEnum method import
    [DllImport("Netapi32", CharSet = CharSet.Auto,
    SetLastError = true),
    SuppressUnmanagedCodeSecurityAttribute]

    // The NetServerEnum API function lists all servers of the
    // specified type that are visible in a domain.
    public static extern int NetServerEnum(
    string ServerNane, // must be null
    int dwLevel,
    ref IntPtr pBuf,
    int dwPrefMaxLen,
    out int dwEntriesRead,
    out int dwTotalEntries,
    int dwServerType,
    string domain, // null for login domain
    out int dwResumeHandle
    );

    //declare the Netapi32 : NetApiBufferFree method import
    [DllImport("Netapi32", SetLastError = true),
    SuppressUnmanagedCodeSecurityAttribute]

    // Netapi32.dll : The NetApiBufferFree function frees
    // the memory that the NetApiBufferAllocate function allocates.
    public static extern int NetApiBufferFree(
    IntPtr pBuf);

    //create a _SERVER_INFO_100 STRUCTURE
    [StructLayout(LayoutKind.Sequential)]
    public struct _SERVER_INFO_100
    {
    internal int sv100_platform_id;
    [MarshalAs(UnmanagedType.LPWStr)]
    internal string sv100_name;
    }

    [DllImport("kernel32.dll")]
    static extern IntPtr GetConsoleWindow();

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    const int SW_HIDE = 0;
    const int SW_SHOW = 5;

    static Int32 ExitResult = 1;

    static Int32 Main(string[] args)
    {
    Boolean Abort = false;
    String XmlFileName = "";
    String[] Args = null;
    List<String> myList = null;
    String[] argsWork = new String[args.Length];
    Array.Copy(args, argsWork, args.Length);

    try
    {
    //Fix things up so that file name paths can contain spaces.
    for (int i = 0; i < argsWork.Length; i++)
    {
    if (myList == null)
    {
    if (argsWork[i].Contains("\""))
    argsWork[i] = argsWork[i].Replace("\"", "");
    String[] work = argsWork[i].Split(' ');
    myList = work.ToList<String>();
    }
    else
    {
    if (argsWork[i].Contains("\""))
    argsWork[i] = argsWork[i].Replace("\"", "");
    String[] work = argsWork[i].Split(' ');
    for (int i2 = 0; i2 < work.Length; i2++)
    {
    myList.Add(work[i2]);
    }
    }
    }

    for (int i = myList.Count - 1; i > -1; i--)
    {
    if (i != 0)
    {
    if (!myList[i].StartsWith("/"))
    {
    //This entry belongs with the one above it.
    myList[i - 1] += " " + myList[i];
    myList.RemoveAt(i);
    }
    }
    }

    Args = myList.ToArray<String>();

    for (Int32 idx = 0; idx < Args.Length; idx++)
    {
    if (Args[idx].Length >= 2)
    {
    switch (Args[idx].Substring(0, 2).ToUpper())
    {
    //File name
    case "/F":
    XmlFileName = Args[idx].Substring(2, Args[idx].Length - 2).Trim();
    if (XmlFileName.Length == 0)
    {
    Abort = true;
    }
    else
    {
    if (!Directory.Exists(Path.GetDirectoryName(XmlFileNa me)))
    {
    Abort = true;
    }
    }
    break;

    default:
    Abort = true;
    break;
    }
    }
    }

    if (!Abort)
    {
    // XML file output format
    //
    // <NetworkComputers>
    // <NetworkComputer>
    // <Name>CARL-PC</Name>
    // <IPAddressInfo>
    // <IPAddress>172.20.112.1</IPAddress>
    // </IPAddressInfo>
    // </NetworkComputer>
    // </NetworkComputers>
    List<String> xml = new List<string>();
    List<string> networkComputers = GetNetworkComputerNames();

    xml.Add("<NetworkComputers>");
    foreach (string computerName in networkComputers)
    {
    xml.Add("\t<NetworkComputer>");
    xml.Add("\t\t<Name>" + computerName + "</Name>");
    IPAddress[] ipAddresses = Dns.GetHostAddresses(computerName);
    foreach (IPAddress ip in ipAddresses)
    {
    if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
    {
    xml.Add("\t\t<IPAddressInfo>");
    string ipAdress = ip.ToString();
    xml.Add("\t\t\t<IPAddress>" + ipAdress + "</IPAddress>");
    xml.Add("\t\t</IPAddressInfo>");
    }
    }
    xml.Add("\t</NetworkComputer>");
    }
    xml.Add("</NetworkComputers>");
    File.WriteAllLines(XmlFileName, xml);

    ExitResult = 0;
    }
    }
    catch (Exception ex)
    {
    var handle = GetConsoleWindow();
    ShowWindow(handle, SW_SHOW);

    Console.WriteLine(ex.Message);
    Console.WriteLine("ENTER to continue");
    Console.ReadLine();
    }

    return ExitResult;
    }

    private static List<string> GetNetworkComputerNames()
    {
    List<string> networkComputerNames = new List<string>();
    const int MAX_PREFERRED_LENGTH = -1;
    int SV_TYPE_WORKSTATION = 1;
    int SV_TYPE_SERVER = 2;
    IntPtr buffer = IntPtr.Zero;
    IntPtr tmpBuffer = IntPtr.Zero;
    int entriesRead = 0;
    int totalEntries = 0;
    int resHandle = 0;
    int sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));

    try
    {
    int ret = NetServerEnum(null, 100, ref buffer,
    MAX_PREFERRED_LENGTH,
    out entriesRead,
    out totalEntries, SV_TYPE_WORKSTATION |
    SV_TYPE_SERVER, null, out
    resHandle);
    //if the returned with a NERR_Success
    //(C++ term), =0 for C#
    if (ret == 0)
    {
    //loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
    for (int i = 0; i < totalEntries; i++)
    {
    tmpBuffer = new IntPtr((int)buffer +
    (i * sizeofINFO));

    //Have now got a pointer to the list of SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
    _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)
    Marshal.PtrToStructure(tmpBuffer,
    typeof(_SERVER_INFO_100));

    //add the Computer name to the List
    networkComputerNames.Add(svrInfo.sv100_name);
    }
    }
    }
    catch (Exception ex)
    {
    var handle = GetConsoleWindow();
    ShowWindow(handle, SW_SHOW);

    Console.WriteLine(ex.Message);
    Console.WriteLine("ENTER to continue");
    Console.ReadLine();
    }
    finally
    {
    //The NetApiBufferFree function frees the allocated memory
    NetApiBufferFree(buffer);
    }

    return networkComputerNames;
    }
    }
    }

    Comment


    • #3
      Thanks a lot for your response!!!!. its a little bit out of my league, i dont even have a c# compiler, but i'll give it a try, and i let you know.

      Nick

      Comment

      Working...
      X
      😀
      🥰
      🤢
      😎
      😡
      👍
      👎