private void btnCancel_Click(object sender, EventArgs e)
    {
        //setDirty();
        this.Close();
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        setDirty();
        //openFileDialog1.ShowDialog();
        saveFileDialog1.ShowDialog();
    }
    private void btnBrowse_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.InitialDirectory = @"";

        openFileDialog1.Title = "Select a File";

        if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
            txtFilePath.Text = openFileDialog1.FileName;
        else

            txtFilePath.Text = "";

    }
    private void saveFileDialog1_FileOk(object sender, EventArgs e)
    {

        SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        StringBuilder sb = new StringBuilder();

        sb.AppendLine(string.Format("chkLoggin{0}\r\n", chkLoggin.Checked));
        sb.AppendLine(string.Format("txtFilePath{0}\r\n", txtFilePath.Text));
        sb.AppendLine(string.Format("txtNumOfThreads{0}\r\n", txtNumOfThreads.Text));

        //string name = saveFileDialog1.FileName
       string name = sb.ToString();
        saveFileDialog1.Title ="";
        saveFileDialog1.OverwritePrompt = true;
        if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            txtFilePath.Text = saveFileDialog1.FileName;


       //File.WriteAllText(name, txtFilePath + Environment.NewLine + txtNumOfThreads.Text);
        File.WriteAllText(txtFilePath.Text, sb.ToString());

      //  WriteLine(sb.ToString());
    }

    private void chkLoggin_CheckedChanged(object sender, EventArgs e)
    {
        setDirty();
        //btnOk.Enabled = chkLoggin.Checked;

    }

    private void txtNumOfThreads_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar)
        && !char.IsDigit(e.KeyChar)
        && e.KeyChar != '.')
        {
            e.Handled = true;
        }

    }

      private void setDirty()
    {
       btnOk.Enabled = chkLoggin.Checked;
        btnOk.Enabled = chkLoggin.Checked && txtFilePath.Text.Length > 0;
        btnOk.Enabled = chkLoggin.Checked && txtNumOfThreads.Text.Length > 0; 
    }



}


using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SettingsFile
{
    public partial class LogForm : Form
    {
        public LogForm()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //setDirty();
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            setDirty();
            //openFileDialog1.ShowDialog();
            saveFileDialog1.ShowDialog();
        }
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @"";

            openFileDialog1.Title = "Select a File";

            if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
                txtFilePath.Text = openFileDialog1.FileName;
            else

                txtFilePath.Text = "";

        }
        private void saveFileDialog1_FileOk(object sender, EventArgs e)
        {

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            StringBuilder sb = new StringBuilder();

            sb.AppendLine(string.Format("chkLoggin{0}\r\n", chkLoggin.Checked));
            sb.AppendLine(string.Format("txtFilePath{0}\r\n", txtFilePath.Text));
            sb.AppendLine(string.Format("txtNumOfThreads{0}\r\n", txtNumOfThreads.Text));

            //string name = saveFileDialog1.FileName
           string name = sb.ToString();
            saveFileDialog1.Title ="";
            saveFileDialog1.OverwritePrompt = true;
            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
                txtFilePath.Text = saveFileDialog1.FileName;


           //File.WriteAllText(name, txtFilePath + Environment.NewLine + txtNumOfThreads.Text);
            File.WriteAllText(txtFilePath.Text, sb.ToString());

          //  WriteLine(sb.ToString());
        }

        private void chkLoggin_CheckedChanged(object sender, EventArgs e)
        {
            setDirty();
            //btnOk.Enabled = chkLoggin.Checked;

        }

        private void txtNumOfThreads_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
            {
                e.Handled = true;
            }

        }

          private void setDirty()
        {
           btnOk.Enabled = chkLoggin.Checked;
            btnOk.Enabled = chkLoggin.Checked && txtFilePath.Text.Length > 0;
            btnOk.Enabled = chkLoggin.Checked && txtNumOfThreads.Text.Length > 0; 
        }



    }
}

Recommended Answers

All 10 Replies

So..it's a code snippet?

no its not

Is there some problem with the code? Or is it merely for imformative purposes?

i am expiriencing a problem, i thought i have attacheches it, i am creating a program that must create, save and open a file, at the same time it must write user's input in a file. will give another code that i have tried but does not work.

So where exactly are you have problems? are you getting exception? Or is the data not displaying in your file? Are you limited to a certain type of file?

Sorry for all the question but it makes it alot easier to help you.

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

Additional information: Access to the path 'C:\mandis.ini' is denied.

thats the error um geting

Access to the path 'C:\mandis.ini' is denied.

You don't have write access to C:\

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            setDirty();

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
           // saveFileDialog1.InitialDirectory = @"C:\";
           // saveFileDialog1.Title = "Select a File";

            //string path = "";
            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
                txtFilePath.Text = saveFileDialog1.FileName;
            else
                txtFilePath.Text = "";

            StringBuilder sb = new StringBuilder();

            sb.AppendLine(string.Format("chkLoggin = {0}\r\n", chkLoggin.Checked));
            sb.AppendLine(string.Format("txtFilePath = {0}\r\n", txtFilePath.Text));
            sb.AppendLine(string.Format("txtNumOfThreads = {0}\r\n", txtNumOfThreads.Text));


            File.WriteAllText( @"C:\\mandis.ini",sb.ToString());
        }
        private void chkLoggin_CheckedChanged(object sender, EventArgs e)
        {
            setDirty();
        }

        private void txtNumOfThreads_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }
        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @"";

            openFileDialog1.Title = "Select a File";

            if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
                txtFilePath.Text = openFileDialog1.FileName;
            else

                txtFilePath.Text = "";
        }

        private void setDirty()
        {
            btnOk.Enabled = chkLoggin.Checked;
            btnOk.Enabled = chkLoggin.Checked && txtFilePath.Text.Length > 0;
            btnOk.Enabled = chkLoggin.Checked && txtNumOfThreads.Text.Length > 0;
        }

    }
}

this is where um expiriencing the error:

File.WriteAllText( @"C:\\mandis.ini",sb.ToString());

'@' means the string following is literal. Either remove the '@' or remove one of the '\'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.