satheesh
satheesh

Reputation: 123

Set a default folder for saving the files in C#

This is my c# code.

But every time I run this code, whenever I click the left click the file is saved in default path : "C:\NewFolder\"

But I don't know how to set the folder selected using right click as my default folder forever.

After I select a folder using right click, whenever I run the exe file, the file should save in that selected folder

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;
        }
    }
}

Upvotes: 2

Views: 8578

Answers (2)

Nikita Shrivastava
Nikita Shrivastava

Reputation: 3018

Add application settings :

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

Read the value from the application settings:

 string folderpath =Properties.Settings.Default.FolderPath ;

When the user right clicks,save the recent settings:

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();
    }
}

Upvotes: 3

VJPPaz
VJPPaz

Reputation: 1015

save your folderpath to your App Settings. then acess it using:

Properties.Settings.Default.[settingName]

Read this article: https://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx

Upvotes: 3

Related Questions