【问题标题】:Set a default folder for saving the files in C#在 C# 中设置用于保存文件的默认文件夹
【发布时间】:2015-12-11 07:29:30
【问题描述】:

这是我的 C# 代码。

但是每次我运行这段代码时,每当我单击左键时,文件都会保存在默认路径中:“C:\NewFolder\”

但我不知道如何将使用右键单击选择的文件夹永久设置为我的默认文件夹。

在我使用右键单击选择一个文件夹后,每当我运行 exe 文件时,该文件应保存在该选定文件夹中

string folderpath = "C:\\NewFolder\\";
private void button1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        bool exists = System.IO.Directory.Exists(folderpath);
        if (!exists)
            System.IO.Directory.CreateDirectory(folderpath);
        this.Hide();
        System.Threading.Thread.Sleep(1000);
        SendKeys.Send("{PRTSC}");
        Image img = Clipboard.GetImage();
        img.Save(folderpath + "\\" + DateTime.Now.Ticks + ".jpg");
        this.Show();
    }
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            folderpath = folderBrowserDialog1.SelectedPath;
        }
    }
}

【问题讨论】:

    标签: c# directory


    【解决方案1】:

    将您的文件夹路径保存到您的应用设置。然后使用:

    Properties.Settings.Default.[settingName]
    

    阅读这篇文章: https://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx

    【讨论】:

    • 这更像是一条评论。
    • 他是完全正确的。您也可以将其存储在 .INI 文件中。只需将其存储在应用程序会话之外的某个位置。并在每次创建会话时加载它。
    • @KevinKal 一旦链接过期,答案将不再解释其意图,因此请始终为链接添加说明。现在看起来好多了。
    • @KevinKal 在链接中添加描述不是喂食!
    • 这正是我要说的。 :P
    【解决方案2】:

    添加应用设置:

    Right click on Project --> Properties --> Settings --> FolderPath | String| User | C:\\NewFolder\\
    

    从应用程序设置中读取值:

     string folderpath =Properties.Settings.Default.FolderPath ;
    

    当用户右键单击时,保存最近的设置:

    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
       DialogResult result = folderBrowserDialog1.ShowDialog();
       if (result == DialogResult.OK)
        {
           Properties.Settings.Default.folderpath = folderBrowserDialog1.SelectedPath;
           Properties.Settings.Default.Save();
        }
    }
    

    【讨论】:

    • `Properties.Settings.Default.Folderpath=folderBrowserDialog1.SelectedPath;'上面代码中的FolderPath是只读的知道的。如何在运行时修改值。
    • 将设置类型更改为用户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多