【问题标题】:Save openFileDialog1.FileName to Properties.Settings将 openFileDialog1.FileName 保存到 Properties.Settings
【发布时间】:2012-12-20 10:37:35
【问题描述】:

我有具有下一个结构的 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

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

【问题讨论】:

    标签: c#


    【解决方案1】:

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

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

    【讨论】:

    • 你能告诉我为什么会这样吗?很有趣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多