Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my program I am saving Sql backup file using C#,
In this program when I click on Button "SAVEDIALOG" open,
But I want to save this file in specific folder or specific path.

Means, I dont want to allow user to save this file in anywhere except specific path.

Kindly help me with it.

Below is coding, on which click event I am saving my file.
Noted: It's a desktop application using C#, SQL server 2008.

C#
private void btnCreate_Click(object sender, EventArgs e)
        {
            // If there was a SQL connection created
            if (srvSql != null)
            {
                // If the user has chosen a path where to save the backup file
                if (saveBackupDialog.ShowDialog() == DialogResult.OK)
                {
                    // Create a new backup operation
                    Backup bkpDatabase = new Backup();
                    // Set the backup type to a database backup
                    bkpDatabase.Action = BackupActionType.Database;
                    // Set the database that we want to perform a backup on
                    bkpDatabase.Database = cmbDatabase.SelectedItem.ToString();

                    // Set the backup device to a file
                    BackupDeviceItem bkpDevice = new BackupDeviceItem(saveBackupDialog.FileName, DeviceType.File);
                    // Add the backup device to the backup
                    bkpDatabase.Devices.Add(bkpDevice);
                    // Perform the backup
                    bkpDatabase.SqlBackup(srvSql);
                }
            }
            else
            {
                // There was no connection established; probably the Connect button was not clicked
                MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
Posted
Updated 15-Jul-13 23:45pm
v5
Comments
[no name] 16-Jul-13 2:36am    
Is it mandatory to open that savefiledialog...directly u can specify the path
Varun_nayak 16-Jul-13 5:32am    
No, its Not mandatory to open savefiledialog,
I just want save my file in specific folder.

well, I got the Solution.. below i mention it

Here The solution:
I add One Textbox in my Form and "txt_FN", in which I am giving My Backup file name,
And Below code saving that file in D drive

C#
private void btnCreate_Click(object sender, EventArgs e)
        {
            // If there was a SQL connection created
            if (srvSql != null)
            {
                // If the user has chosen a path where to save the backup file
               
                string ext = ".bak";
                
  saveBackupDialog.Title = "SAVE a Backupfile";
  saveBackupDialog.FileName = @"D:\\";
                
         
                
                saveBackupDialog.InitialDirectory = @"D:";

                
                    // Create a new backup operation
                   
Backup bkpDatabase = new Backup();
                   
 // Set the backup type to a database backup

  bkpDatabase.Action = BackupActionType.Database;

  // Set the database that we want to perform a backup on
    bkpDatabase.Database = cmbDatabase.SelectedItem.ToString();

                  
  // Set the backup device to a file
                   
 BackupDeviceItem bkpDevice = new BackupDeviceItem(saveBackupDialog.FileName+txt_FN.Text.ToString()+ext, DeviceType.File);
                    
// Add the backup device to the backup
                  
  bkpDatabase.Devices.Add(bkpDevice);
                 
   // Perform the backup
                
    bkpDatabase.SqlBackup(srvSql);
                
    MessageBox.Show("BACKUP SAVED SUCCESSFULLY");
              
 
            }
            else
            {
             
   // There was no connection established; probably the Connect button was not clicked
            
    MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
 
Share this answer
 
v3
as per above solution.

I solve it my self.
 
Share this answer
 
Comments
Nirav Prabtani 16-Jul-13 6:32am    
congrats but do not write like that as a solution please mention it as comment...please remove it from here..>:)

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