2017-03-09 5 views
-1

データをプリンタに送信するためのシリアルプログラムがあります。 Epsonユーティリティを使用して、USB、パラレル、イーサネットプリンタをCOMポート(Epson TM仮想ポート割り当てツールVer 8.50)に再割り当てします。 この場合、USBプリンタ用のCOM9、Ethernetプリンタ用のCOM13、パラレルプリンタ。私はそれが奇妙に聞こえることは知っていますが、それは素晴らしいです。 私の問題は、COMポート名を取得するための単純なforeachループがあることです。 私はコンボボックスで名前を変更する方法を知る必要があります。 プライベート文字列[] openComPorts = SerialPort.GetPortNames(); foreach(openComPortsのvarアイテム) { comboBox1.Items.Add(item); }COMポートの名前を一度変更して名前を変更しました

私のcomboBox1は、COM1、COM3、COM4、COM9、COM13、COM15を表示します。 COM9,13,15の名前を変更するにはどうすればよいですか? 私は彼らにCOM9-USB、COM13-Ethernet、COM15-Parallelと言ってほしい。 何か助けていただければ幸いです。 ターゲットPCでWindows 7を実行している、.NET 4.xでは、SP1は

+0

は、あなたが使用するカスタム名を知っていますか事前に? (I.E.COM9-USB) – BackDoorNoBaby

+0

はい、COM9-USB、COM13-Ethernet、COM15-Parallelのように、ユーザーはどちらを記憶する必要はありません。 –

+0

あなたはコンボボックス内のすべてのCOMポートを必要としますか、カスタム名を使用するポートだけが必要ですか? – BackDoorNoBaby

答えて

0

はちょうどあなたのポートのためのルックアップテーブルを使用する辞書を作る:

 string[] openComPorts = SerialPort.GetPortNames(); 
    Dictionary<string, string> dctLookups = new Dictionary<string, string>(); 

    // Loop through each COM port name and add to dictionary, giving 
    // it your custom name 
    foreach (string comPort in openComPorts) 
    { 
     // local variable to hold standard name of your port 
     string portName = comPort; 

     // Check for one of the ports you want a custom name for 
     if (comPort == "COM9") 
     { 
      portName += "-USB"; 
     } 
     else if (comPort == "COM13") 
     { 
      portName += "-Ethernet"; 
     } 
     else if (comPort == "COM15") 
     { 
      portName += "-Parallel"; 
     } 

     // Add to <key,value> dictionary with key being portName 
     dctLookups.Add(portName, comPort); 

     // Add custom name to combobox 
     comboBox1.Items.Add(portName); 
    } 

    // Get the actual COM port out of your dictionary like this 
    try 
    { 
     string comPort = dctLookups[comboBox1.SelectedItem.ToString()]; 
    } 
    catch (Exception) 
    { 
     // Port name not in your lookup table 
    } 
+0

これは、キーが割り当てたカスタム値であるテーブルを維持することを可能にし、値は実際のCOMポート名です。あなたが気にしないCOMポートについては、キーと値は同じになります – BackDoorNoBaby

+1

Worked 100%、Exectly私が探していたもの、ありがとうございます! :) –