如何获取打印机的网络路径 [英] How to obtain the network path of a printer

查看:95
本文介绍了如何获取打印机的网络路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要能够获取网络上所有打印机的网络路径(\\ computername \ printershare).我已经在网上搜索并找到了很多有关如何获取网络打印机列表的示例,但是我一生都无法解决如何获取网络路径的问题.

我正在使用的代码是

Hi,
I need to be able to obtain the network paths (\\computername\printershare) of all printers on our network. I have web-searched and found quite a few examples of how to obtain a list of network printers, but I cannot for the life of me work out how to get the network path.

The code I am using is

public static List<string> GetNetworkPrinters()
{
    List<string> prtList = new List<string>();
    DirectoryEntry dirEntry;
    DirectorySearcher dirSearcher;
    SearchResultCollection resultCollection;
    string debug;
    try
    {
        dirEntry = new DirectoryEntry("");
        dirSearcher = new DirectorySearcher(dirEntry);
        dirSearcher.PageSize = 10;
        dirSearcher.Filter = "objectCategory=printQueue"; // search filter
        dirSearcher.PropertyNamesOnly = true;
        dirSearcher.PropertiesToLoad.Add("Name");
        dirSearcher.SearchScope = SearchScope.Subtree;
        resultCollection = dirSearcher.FindAll();
        foreach (SearchResult result in resultCollection)
        {
            string path = result.GetDirectoryEntry().Path.ToString();
            prtList.Add(result.GetDirectoryEntry().Name.Substring(3));
        }
    }
    catch (Exception ex)
    {
        // MsgBox(ex.Message)
    }
    return prtList;
}




变量``path''接近了,但是似乎用打印机名称代替了打印机注释,这对我没有帮助.

任何帮助表示赞赏!

-David.




The ''path'' variable gets close, but it seems to be substituting the printer name for the printer comment which is of no help to me.

Any help appreciated!!

-David.

推荐答案

我认为您的问题是获取打印机的共享名.上面的方法将返回以其开头的计算机名称.

一种方法是在对命令提示符功能的调用中进行编码.获取打印机所在计算机的名称.然后,对于每台计算机,运行
I assume that your problem is getting the shared name of the printer. The method above will return the computer name to start with.

One method would be to code in a call to a command prompt function. Get the name of the computers that the printers are on. Then, for each computer, run
Net View \\(computer name)


然后,在结果中搜索打印机.这将为您提供简称. VB.Net中的示例:


Then, search through the results for the printers. That will give you the short names. Example in VB.Net:

Try
    dirEntry = New DirectoryEntry("")
    dirSearcher = New DirectorySearcher(dirEntry)
    dirSearcher.PageSize = 10
    dirSearcher.Filter = "objectCategory=printQueue"
    dirSearcher.PropertyNamesOnly = True
    dirSearcher.PropertiesToLoad.Add("Name")
    dirSearcher.SearchScope = SearchScope.Subtree
    resultCollection = dirSearcher.FindAll
    For Each result As SearchResult In resultCollection
        Dim values() As String = Strings.Split(path, ",")
        For Each st In values
            If Left(st, 3) = "CN=" Then
                Dim pInfo As New ProcessStartInfo
                pInfo.FileName = "Net"
                pInfo.Arguments = "View \\" & Right(st, st.Length - 3)
                pInfo.UseShellExecute = False
                pInfo.CreateNoWindow = True
                pInfo.RedirectStandardOutput = True
                Dim p As Process = Process.Start(pInfo)
                Dim pReader As System.IO.StreamReader = p.StandardOutput
                MessageBox.Show(pReader.ReadToEnd)
                p.Close()
            End If
        Next
    Next
Catch ex As Exception
End Try


这篇关于如何获取打印机的网络路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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