2 ccx527191915 ccx527191915 于 2016.01.25 14:31 提问

c#打开文件夹程序怎么打开windows默认的文件夹
c#
 private void pictureBox9_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(".\\Background") == false)
            {
                Directory.CreateDirectory(".\\Background");
                OpenFileDialog of1 = new OpenFileDialog();
                of1.InitialDirectory = "c:\\";
                of1.Filter = "png|*.png|jpg|*.jpg|bmp|*.bmp";
                of1.RestoreDirectory = true;
                of1.FilterIndex = 1;
                if (of1.ShowDialog() == DialogResult.OK)
                {
                    picfile = of1.FileName;
                    picName = of1.SafeFileName;
                    try
                    {
                        File.Copy(picfile, string.Format("Background\\{0}", picName, true));
                    }
                    catch (Exception)
                    {

                        //
                    }
                    this.BackgroundImage = Image.FromFile(string.Format("Background\\{0}", picName));
                }
            }
            else
            {
                OpenFileDialog of = new OpenFileDialog();
                of.InitialDirectory = "c:\\";
                of.Filter = "png|*.png|jpg|*.jpg|bmp|*.bmp";
                of.RestoreDirectory = true;
                of.FilterIndex = 1;
                if (of.ShowDialog() == DialogResult.OK)
                {
                    picfile = of.FileName;
                    picName = of.SafeFileName;
                    try
                    {
                        File.Copy(picfile, string.Format("Background\\{0}", picName, true));
                    }
                    catch (Exception)
                    {

                        //
                    }
                    this.BackgroundImage = Image.FromFile(string.Format("Background\\{0}", picName));
                }
            }

        }

我有这样的一个程序,每次都是打开C盘。我想让他能够记忆我曾经打开的文件夹路径。就像用浏览器保存图片一样,每次右键出来的选择文件件路径都是一样的。

2个回答

caozhy
caozhy   Ds   Rxr 2016.01.26 05:07
 string path = Application.StartupPath;
try
{
    path = System.IO.File.ReadAllText("path.txt");
}
catch { }
OpenFileDialog of = new OpenFileDialog();
of.InitialDirectory = path;
...
if (of.ShowDialog() == DialogResult.OK)
{
path = System.IO.Path.GetDirectoryName(of.FileName);
System.IO.File.WriteAllText("path.txt", path);
...
}

你需要有一个外部文件保存和记忆这个路径,参考我的代码

91program
91program   Ds   Rxr 2016.01.25 14:36

不要做这个赋值:of1.InitialDirectory = "c:\";

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!