2015-09-15 88 views
1

這是我的C#代碼。設置保存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; 
     } 
    } 
} 

回答

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

'Properties.Settings.Default.Folderpath = folderBrowserDialog1.SelectedPath;'以上代碼中的FolderPath是隻讀的。如何在運行時修改該值。 – satheesh

+0

將設置類型更改爲用戶。 –

+0

供您參考:http://stackoverflow.com/questions/758389/why-are-application-settings-read-only-in-app-config –

2

您FOLDERPATH保存到您的應用程序設置。然後接取它使用:

Properties.Settings.Default.[settingName] 

閱讀這篇文章: https://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx

+1

這更像是一條評論。 –

+0

他完全正確。您也可以將其存儲在.INI文件中。只需將它存儲在應用程序會話之外的某個位置。並在每次創建會話時加載它。 – kevintjuh93

+0

@KevinKal一旦鏈接過期,答案將不再解釋意圖,因此請始終爲鏈接添加說明。現在看起來更好。 –