将openFileDialog1.FileName保存到Properties.Settings

时间:2012-12-20 10:37:35

标签: c#

我有下一个结构的WFA:

using System;
using System.Collections.Generic;
using System.­­­­ComponentModel;
using Sy­­­­stem.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WindowsFormsApplication1.Properties;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<string> urls = new List<string>();
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = System.Environment.CurrentDirectory;
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                urls.Clear();
                foreach (string url in File.ReadAllLines(openFileDialog1.FileName))
                    urls.Add(url);
                label2.Text = urls.Count.ToString();
            }
        }

        List<string> links = new List<string>();
        private void open2FileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog open­­­­FileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = System.Environment.CurrentDirectory;
            openFile­­­­Dialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                links.Clear();
                foreach (string link in File.ReadAllLines(openFileDialog1.FileName))
                    links.Add(link);
                label4.Text = links.Count.ToString();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Settings.Default.Save();
        }
    }
}

设计屏幕:

http://screenshotuploader.com/i/01/k0tpbijza.png

我需要保存到propeties.settings文件的路径,在openFileDialog中打开。

1 个答案:

答案 0 :(得分:0)

您需要将RestoreDirectory设置为true以及InitialDirectory属性。

有关该主题的更多信息:Setting the initial directory of an SaveFileDialog?