Click here to Skip to main content
15,877,915 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to create a program such that if the file exists, the checkbox will auto check.


What I have tried:

C#
private void autoCheckChkBoxes(CheckBox theChkBox, TextBox theTxtBox)
    {
       private void button3_Click(object sender, EventArgs e)
{
  //check whether the file is exist or not
  if (string curFile = @"C:\s\test.txt")
  {

  }
  else
  {

  }
}
Posted
Updated 21-Jul-17 21:29pm
Comments
Graeme_Grant 21-Jul-17 22:01pm    
Is this a WinForm, WPF, or Xamarin app?
Asyraf Patt 21-Jul-17 22:09pm    
Winform
Graeme_Grant 21-Jul-17 22:12pm    
see below... Please mark as solved if you find it useful.

C#
private void button3_Click(object sender, EventArgs e)
{
    autoCheckChkBoxes(checkBoxObj,TextBox);
}
private void autoCheckChkBoxes(CheckBox theChkBox, TextBox theTxtBox)
{
   string curFile = @"C:\s\test.txt"
   theChkBox.checked=system.IO.File.Exist(curFile);
}

Hope it works for you.
 
Share this answer
 
v2
Have you tried using google search? c# check if file exists - Google Search[^] - there are a tonne of examples of how to do what you want...

C#
theChkBox.IsChecked = System.IO.File.Exists(theTxtBox.Text);


** UPDATE **

How to do it automatically...

C#
private void theTextBox_TextChanged(object sender, EventArgs e)
{
    theCheckBox.Checked = System.IO.File.Exists(theTextBox.Text);
}
 
Share this answer
 
v3
Comments
Asyraf Patt 21-Jul-17 22:14pm    
checkBox10.Checked = System.IO.File.Exists(@"c:\path\to\file.txt") ? true : false;

this is my solution so far. If the file exist, the output will show"file exist" but the check box still remain uncheck
Graeme_Grant 21-Jul-17 22:17pm    
System.IO.File.Exists already returns true or false, so you don't need to test the boolean result - see my WPF code - Winform is very similar.
Asyraf Patt 21-Jul-17 22:24pm    
checkBox1.Checked = System.IO.File.Exists(@"c:\path\to\files.txt");

like this ? but the checkbox still remain uncheck. why ?
Graeme_Grant 21-Jul-17 22:34pm    
Look above at my answer ... It might be a good idea to learn how to use the debugger so that you can check what your code is actually doing:

Basic Debugging with Visual Studio 2010 - YouTube[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900