Скачать проект в VS2010
//Добавил много нового для удобства работы с программой.
// The name of the key must include a valid root.
const string userRoot = "HKEY_CURRENT_USER";
const string subkey = "CanScanner";
const string keyName = userRoot + "\\" + subkey;
//Номер компорта берем из реестра, где ранее сохранили.
serialPort1.PortName = (string)Registry.GetValue(keyName, "NumComPort", "COM1");
//Добавляем в проект новую форму - пкм в Решении Проекта на названии проекта - добавить форму
private void buttonSetupCOMport_Click(object sender, EventArgs e)
{
SetupComPort setupComPort = new SetupComPort();
// Define the border style of the form to a dialog box.
setupComPort.FormBorderStyle = FormBorderStyle.FixedDialog;
// Set the MaximizeBox to false to remove the maximize box.
setupComPort.MaximizeBox = false;
// Set the MinimizeBox to false to remove the minimize box.
setupComPort.MinimizeBox = false;
// Set the start position of the form to the center of the screen.
setupComPort.StartPosition = FormStartPosition.CenterScreen;
setupComPort.ShowDialog();
}
//Добавим кнопку и компонент на форму для сохранения на комп нашего TextBox1
private void buttonSave_Click(object sender, EventArgs e)
{
saveFileDialog1.DefaultExt = "*.txt";
saveFileDialog1.Filter = "Блокнот|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.FileName, textBox1.Text);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace CanScannerV102
{
public partial class SetupComPort : Form
{
// The name of the key must include a valid root.
const string userRoot = "HKEY_CURRENT_USER";
const string subkey = "CanScanner";
const string keyName = userRoot + "\\" + subkey;
public SetupComPort()
{
InitializeComponent();
string numCom = "COM";
for (int i = 1; i <= 255; i++)
{
numPort.Items.Add(numCom += i.ToString("d"));
numCom = "COM";
}
}
private void buttonClose_Click(object sender, EventArgs e)
{
//Закрываем Форму
this.Close();
}
private void numPort_SelectedIndexChanged(object sender, EventArgs e)
{
Registry.SetValue(keyName, "NumComPort", numPort.Text);
}
}
}