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



    }
}

Dani AI

Generated

Summary and focused fixes based on the thread (error: "Access to the path 'C:\mandis.ini' is denied") — was correct that writing to the root of C: is often blocked for standard users, and correctly pointed out the literal-string/backslash confusion. There is also a logic/flow issue in the dialog handling: creating or re-showing a SaveFileDialog inside its FileOk handler will break the expected flow. The setDirty() code also overwrites its own checks instead of combining them.

Prefer writing to a user-writable location (AppData or Documents) or use the filename returned by SaveFileDialog; always check the dialog result and handle exceptions. Example patterns:

Write to an app data folder (safe default):

var folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyAppName");
Directory.CreateDirectory(folder);
var path = Path.Combine(folder, "mandis.ini");
File.WriteAllText(path, sb.ToString());

Use the SaveFileDialog result (do not instantiate/show a new dialog inside FileOk):

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    try
    {
        File.WriteAllText(saveFileDialog1.FileName, sb.ToString());
    }
    catch (UnauthorizedAccessException ex)
    {
        MessageBox.Show("Cannot write file: " + ex.Message);
    }
}

Fix the UI-enable logic by combining conditions into one assignment instead of three overwrites:

btnOk.Enabled = chkLoggin.Checked && txtFilePath.Text.Length > 0 && txtNumOfThreads.Text.Length > 0;

Notes and troubleshooting: running as Administrator will confirm a permissions problem but is not a deployment fix. Also check file attributes (read-only), locks by other processes, and antivirus. Tying the above to the thread: follow 's advice on string literals, accept 's permissions point, and remove the extra SaveFileDialog usage in the FileOk handler to keep the flow predictable.

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.