Hello,

I'm very new to C#. Basically, this is an existing windows form application, I'm supposed to add a checkbox in two existing forms. The forms have other comboxes and and textboxes. There's a restricted folder for many departments.So, when i fill up the other fields and check this check box, the file has to sit in the related restricted folder in windows. How do do this??? Can anybody help me?

Thanks,

Shilpa

Dani AI

Generated

Short plan and pitfalls (for , following 's note about file I/O)

Implement this at your form's Save/Upload action:

  • Decide target folder from the checkbox (restricted vs normal).
  • Create the folder if missing and sanitize the filename.
  • Copy the incoming file to a temporary name inside the target folder.
  • Update your table (set the restricted flag and store the file path) in the database.
  • If the DB update succeeds, rename the temp file to the final name. If anything fails, delete the temp file and roll back or clear the DB change.

This keeps file-system state and DB state in sync and avoids leaving a sensitive file outside the restricted folder if a DB write fails.

Example (WinForms C# sketch)

private void btnSave_Click(object sender, EventArgs e)
{
    bool isRestricted = chkRestricted.Checked;
    string sourcePath = txtSourcePath.Text;          // e.g. from OpenFileDialog
    string targetRoot = isRestricted ? @"C:\Restricted\DeptA" : @"C:\Public\DeptA";
    Directory.CreateDirectory(targetRoot);

    string fileName = SanitizeFileName(Path.GetFileName(sourcePath));
    string finalPath = GetUniqueFilePath(targetRoot, fileName);
    string tempPath = finalPath + ".tmp";

    try
    {
        File.Copy(sourcePath, tempPath, true);

        // update DB (use parameterized SQL and a transaction)
        using (var conn = new SqlConnection(connectionString))
        {
            conn.Open();
            using (var tx = conn.BeginTransaction())
            using (var cmd = conn.CreateCommand())
            {
                cmd.Transaction = tx;
                cmd.CommandText = "UPDATE Documents SET IsRestricted=@r, FilePath=@p WHERE DocumentId=@id";
                cmd.Parameters.AddWithValue("@r", isRestricted);
                cmd.Parameters.AddWithValue("@p", finalPath);
                cmd.Parameters.AddWithValue("@id", docId);
                cmd.ExecuteNonQuery();
                tx.Commit();
            }
        }

        File.Move(tempPath, finalPath);
    }
    catch (Exception ex)
    {
        if (File.Exists(tempPath)) File.Delete(tempPath);
        // log and surface the error
    }
}

static string SanitizeFileName(string name)
{
    foreach (char c in Path.GetInvalidFileNameChars()) name = name.Replace(c, '_');
    return name;
}

Practical notes and cautions

  • The running user must have write permission on the restricted folder; for network shares use UNC paths and appropriate credentials.
  • Validate file type/size and avoid AddWithValue for production (use explicit SqlParameter types).
  • If you need OS-level access control, configure NTFS ACLs (do this at admin time; programmatic ACL changes are possible via DirectorySecurity) — see the .NET docs on DirectorySecurity for examples (DirectorySecurity docs).

Recommended Answers

All 2 Replies

This is I need to do. I want to add a check box to the form that will allow user to update column as restricted in a table. If this option is selected the file should be stored inside the restricted folder.

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.