Regular expressions and printers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    Regular expressions and printers

    Hi

    I have never used regular expressions and before I start playing I'm hoping some one can save me hours of work with advice: can I use regular expressions in this case?

    I would like to print to a printer over terminal services. The issue with terminal servers and printing is the server automatically assigns an ID to the printer name. Example, my local printer is called HPprinter. When I log onto the server, it is assigned a new name "HPprinter in session 5". I have no control of the session number.

    What I would like to do is print to a printer called "HPprinter% " (where the % is used similary to a wildcard in SQL). So, when I log on it doesn't matter which session number is assigned because the first part of the printer is 'fixed', the rest is incorporated into the wild card.

    Within the permissions of the Terminal Server I can set it up so it only sees the users local printers to avoid confusing the server about which printer to print too.

    The way my mind works this out is to find out what printers are installed and loop through them

    Code:
    foreach (string strP in PrinterSettings.InstalledPrinters)
    {
          label10.Text += strP + Environment.NewLine;
    
    }
    and to then add the regex in the middle some where and some how.

    Code:
    string printerToUse
    foreach (string strP in PrinterSettings.InstalledPrinters)
                {
                    if (strP starts of hpPrinter) //I assume regex is used here
                    {
                          printerToUse = strP;
                          break;
                    }
                }
    So, my question is, can it be done?

    Thanks

    Dave
  • DaveRook
    New Member
    • Jul 2007
    • 147

    #2
    Code:
     Regex r = new Regex("QL-580");
                    string selectedPrinter = string.Empty;
                    foreach (string strP in PrinterSettings.InstalledPrinters)
                    {
    
                        Match m = r.Match(strP);
                        if (m.Success)
                        {
                            selectedPrinter = strP;
                            break;
                        }
                    }
    Sorted!

    Comment

    Working...