Click here to Skip to main content
14,383,633 members
Rate this:
Please Sign up or sign in to vote.
Hello all,

I am working on an application that connects to a unc path with credentials. My application scans the network for shared computers then the user selects the computer from a listbox and from the listbox the IP address goes to a textbox.

In this textbox resides the IP address I would like to know how to open a unc path from a textbox on button click I have some code below to show what I have so far. Button1 is supposed to browse the unc path from IPtxt (which is a textbox).

If someone can point me in the right direction or an example of how I can go about doing this it would be greatly appreciated. Thank you for your time.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Net.Configuration;
using System.Runtime.InteropServices;

namespace ListNetworkComputers
{
    /// <summary>
    /// A simply test form that creates a new NetworkBrowser
    /// object, and displays a list of the network computers
    /// found by the NetworkBrowser
    /// </summary>
    public partial class frmMain : Form
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public frmMain()
        {
            InitializeComponent();
        }


        private void frmMain_Load(object sender, EventArgs e)
        {

            
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void Scanbtn_Click(object sender, EventArgs e)
        {
            Process netUtility = new Process();

            netUtility.StartInfo.FileName = "net.exe";

            netUtility.StartInfo.CreateNoWindow = true;

            netUtility.StartInfo.Arguments = "view";

            netUtility.StartInfo.RedirectStandardOutput = true;

            netUtility.StartInfo.UseShellExecute = false;

            netUtility.StartInfo.RedirectStandardError = true;

            netUtility.Start();



            StreamReader streamReader = new StreamReader(netUtility.StandardOutput.BaseStream, netUtility.StandardOutput.CurrentEncoding);



            string line = "";

            while ((line = streamReader.ReadLine()) != null)
            {
                if (line.StartsWith("\\"))
                {


                    string pcname = line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper();
                    string myIP = Convert.ToString(System.Net.Dns.GetHostByName(line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper()).AddressList[0].ToString());
                    string fullname = "PC Name : " + pcname + " IP Address : " + myIP;
                    listBox1.Items.Add(pcname);
                    listBox2.Items.Add(myIP);
                }
            }

            streamReader.Close();
            netUtility.WaitForExit(1000);


        }
       
        private void button1_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

            string ipaddress = listBox2.GetItemText(listBox2.SelectedItem);
            System.Net.IPAddress ip = System.Net.IPAddress.Parse(ipaddress);
            
            IPtxt.Text = listBox2.SelectedItem.ToString();

           // string text = listBox2.GetItemText(listBox2.SelectedItem);
            
          //  label1.Text = listBox2.SelectedItem.ToString();

        }

        private void cTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}


What I have tried:

I the code attached to my question. Thank you for taking your time looking over my code and helping me kind regards.
Posted
Updated 30-Aug-18 5:50am
Comments
Richard MacCutchan 30-Aug-18 3:47am
   
Lots of code there but no real explanation of where the problem lies. Are you saying that you do not know how to access the data from a textbox, or that you do not know how to create a UNC path from an IP address string?
Donald jeffers 30-Aug-18 12:17pm
   
What I need is help with creating a unc path from an IP address string that I have in my code. which is this right here. System.Net.IPAddress ip = System.Net.IPAddress.Parse(ipaddress); It goes to the textbox but I don't know how to create a unc path from a textbox value.
Richard MacCutchan 30-Aug-18 12:52pm
   
Use a string bulider.

StringBuilder uncPath = new StringBuilder("\\\\");
uncPath.Append(IPTextBox.Text);
uncPath.Append("\\directory");
... etc.
Donald jeffers 30-Aug-18 13:48pm
   
okay, thank you very much. my only question I have left is how do I get it to open a window such as windows explorer?
Richard MacCutchan 30-Aug-18 15:40pm
   
Sorry I am not sure of the answer to that. Maybe Google has some ideas.
Donald jeffers 30-Aug-18 17:51pm
   
string line = "";

while ((line = streamReader.ReadLine()) != null)
{
if (line.StartsWith("\\"))
{


string pcname = line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper();
string fullPath = Convert.ToString(System.Net.Dns.GetHostByName(line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper()).AddressList[0].ToString());
string fullname = "PC Name : " + pcname + " IP Address : " + fullPath;

Process process = new Process();
// string fullPath = IPtxt.ToString();
process.StartInfo.FileName = "\\\\" + fullPath + "\\";
process.StartInfo.UseShellExecute = true;
process.StartInfo.ErrorDialog = true;
process.Start();
I was able to figure it out with the code above. Thank you for your help appreciate it. :D
Richard MacCutchan 31-Aug-18 3:04am
   
string fullPath = Convert.ToString(System.Net.Dns.GetHostByName(line.Substring(2).Substring(0, line.Substring(2).IndexOf(" ")).ToUpper()).AddressList[0].ToString());
Lines of code like that are extermely difficult to debug if any part fails.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If I get your question properly you want to open a unc folder via you application?

if so the following stackoverflow may help as it opens a windows explorer for a given path

vb.net - Open remote shared folder with credentials - Stack Overflow[^]

I know that it is VB.NET but should be very easily converted to C#
   
Comments
Donald jeffers 30-Aug-18 12:16pm
   
What I need is help with creating a unc path from an IP address string that I have in my code. which is this right here. System.Net.IPAddress ip = System.Net.IPAddress.Parse(ipaddress); It goes to the textbox but I don't know how to create a unc path from a textbox value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100