Scalar

态度决定一切,正能量走起!

导航

使用Settings保存上次选择路径

1、新建Windows窗体应用程序,eg:SettingsDemo

2、窗体布局

3、右击SettingsDemo项目→属性→设置(设置如下保存)

4、窗体加载事件、按钮单击事件

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 SettingsDemo.Properties;

namespace SettingsDemo
{
/// <summary>
/// 保存路径
/// </summary>
public partial class Form1 : Form
{
  private string path = string.Empty;
  public Form1()
  {
    InitializeComponent();
  }

  private void btnSelect_Click(object sender, EventArgs e)
  {
    FolderBrowserDialog browserDialog = new FolderBrowserDialog();
    browserDialog.SelectedPath = path;
    if (browserDialog.ShowDialog() == DialogResult.OK)
    {
      SetPath(browserDialog.SelectedPath);
    }
  }

  private void Form1_Load(object sender, EventArgs e)
  {
    path = Settings.Default.DefaultPath;
    txtPath.Text = path;
  }

  //设置路径

  private void SetPath(string _path)
  {
    path = _path;
    txtPath.Text = path;
    Settings.Default.DefaultPath = path;
    Settings.Default.Save();
  }
 }
}

 

posted on 2012-05-02 18:18  Scalar  阅读(119)  评论(0)    收藏  举报