如何使用C#在特定文件夹中保存(.bak)文件 [英] How to save (.bak) file in specific folder using C#

查看:106
本文介绍了如何使用C#在特定文件夹中保存(.bak)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中我使用C#保存Sql备份文件,

在这个程序中,当我点击按钮SAVEDIALOG打开时,

但是我想保存这个特定文件夹或特定路径中的文件。



意味着,我不想让用户将此文件保存在除特定路径之外的任何位置。



请帮助我。



以下是编码,点击事件我保存文件。

注意:这是一个使用C#,SQL Server 2008的桌面应用程序。



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.

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

推荐答案

这里解决方案:

我添加一个文本框在我的表格和txt_FN中,我在其中提供我的备份文件名,

和以下代码在D盘中保存该文件



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

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


按照上述解决方案。



我解决它自己。
as per above solution.

I solve it my self.


这篇关于如何使用C#在特定文件夹中保存(.bak)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆