怎么做我从文本文本框中获取unc路径值 [英] how to do i get the unc path value from text textbox

查看:96
本文介绍了怎么做我从文本文本框中获取unc路径值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello all,



我在工作在使用凭据连接到unc路径的应用程序上。我的应用程序扫描网络以查找共享计算机,然后用户从列表框中选择计算机,并从列表框中将IP地址转到文本框。


$
在这个文本框中保留了我想知道如何在按钮点击时从文本框打开unc路径的IP地址我在下面有一些代码展示我到目前为止所拥有的。 Button1应该从IPtxt(这是IP地址
所在的文本框)浏览unc路径。

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 the textbox that the IP address resides in).

如果有人可以指出我正确的方向或我如何做到这一点的一个例子,将不胜感激。感谢您的时间。

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)
        {

        }
    }
}




推荐答案

UNC路径不仅仅是一个IP地址,因此您没有足够的信息来做任何事情。 UNC路径将由文件共享支持。文件共享由其定义的机器和路径本身组成。 IP地址只能为您提供该等式的一半
。如果没有文件共享名,则无法连接到路径。

A UNC path isn't just an IP address so you don't have sufficient information to do anything. A UNC path is going to be backed by a file share. A file share consists of the machine it is defined on and the path itself. An IP address only gives you the first half of that equation. You cannot connect to the path without the file share name.

您可以根据网络地址获取计算机上的可用共享。但.NET不公开网络共享支持。您必须使用第三方库或P / Invoke。要获取文件共享,您可以使用

NetShareEnum
。不幸的是,由于缓冲区,使用Net API在托管代码中有点困难。我建议你谷歌使用NetShareEnum和C#来获取有关如何操作的代码的参考。

You can get the available shares on a machine given its network address. But .NET does not expose network share support. You'll have to either use a third party library or P/Invoke. To get the file shares you can use NetShareEnum. Unfortunately working with the Net API is a little tough in managed code because of the buffers. I recommend you google for using NetShareEnum with C# to get references to code on how to do it.

确定机器和共享后,你可以直接使用UNC路径大多数.NET调用文件或创建文件共享。如果你去文件共享路由,你将不得不再次调用Win32。 UNC路径可能是更简单的方法,但是如果它需要任何类型的凭证,则
然后调用将失败。 .NET不提供对当前用户之外的UNC路径进行身份验证的方法。

After you've identified the machine and share you can then either use the UNC path directly in most file calls in .NET or create a file share. If you go the file share route you'll have to again call Win32. The UNC path is probably the simpler approach but if it requires credentials of any sort then the calls will fail. .NET doesn't provide a way to authenticate to a UNC path outside the current user.


这篇关于怎么做我从文本文本框中获取unc路径值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆