Developing batch Access database compression software with. Net C #

Source: Internet
Author: User
Tags array copy net string tostring access database access root directory
The first write uses. NET C # to develop a slightly like software, in the software development process I looked at a lot of information, but also learned many tips

Like FolderBrowserDialog (used to browse the dialog box for the selection of folders), MessageBox (Message Processing dialog), DirectoryInfo (directory information, can be used to create, detect the existence of a directory operation, etc.), FileInfo (file information, Can be used for file detection, file information acquisition, replication and other operations), DataGridView (data table control, used to display file information list data), DataRowView (some data source information to filter, sort), System.Diagnostics.Process.Start (Start another program to open the folder directory), here is a brief introduction to this software development I have used the above controls, what the object content.

A,FolderBrowserDialog (Folder Browsing dialog box), in this software to open the Select database root or open the Create, select a backup directory, the following is a two-place code details.

1. Select the database directory where you do not need to create a new folder, so mask the new Folder button.

C # code
  1. FolderBrowserDialog df = new FolderBrowserDialog ();
  2. To set the description on the File Browsing dialog box
  3. Df. Description = "Select the address of the root directory where all database files reside";
  4. Create a new Folder button below the dialog box is not displayed
  5. Df. Shownewfolderbutton = false;
  6. /*
  7. Determines whether the folder directory address is entered directly, and if so, assigns the value to the selected address of the dialog box so that the dialog box displays the directory address that you last selected or added.
  8. */
  9. if (Tboxdbroot.text!= "")
  10. {
  11. Df. SelectedPath = Tboxdbroot.text;
  12. }
  13. Else
  14. {
  15. Df. RootFolder = environment.specialfolder.mycomputer;//Specifies the address of the root directory displayed by default in the dialog box note rootfolder receive data type
  16. }
  17. Displays the Folder dialog box and returns the dialog box to process the result value
  18. DialogResult result = df. ShowDialog ();
  19. if (result = = DialogResult.OK)//Another method of judging if (DF. ShowDialog (This) = = DialogResult.OK)
  20. {
  21. To assign the database directory address in to the class global variable database root directory
  22. String folderpath = df. SelectedPath;
  23. if (FolderPath!= "")
  24. {
  25. Tboxdbroot.text = FolderPath;
  26. Cls_dbrootpath = Tboxdbroot.text;
  27. }
  28. }

2. Select a database backup directory or create a new database backup directory

C # code
  1. FolderBrowserDialog Bakfolder = new FolderBrowserDialog ();
  2. Bakfolder.description = "Select all database file backup directory";
  3. There is no design Bakfolder.shownewfolderbutton because the default buttons are displayed.
  4. if (Cls_dbbackrootpath!= "")
  5. {
  6. Bakfolder.selectedpath = Cls_dbbackrootpath;
  7. }
  8. Else
  9. {
  10. Bakfolder.rootfolder = Environment.SpecialFolder.MyComputer;
  11. }
  12. if (Bakfolder.showdialog (this) = DialogResult.OK)
  13. {
  14. Cls_dbbackrootpath = Bakfolder.selectedpath;
  15. This omits the code to start processing the database backup ...
  16. }

Second,MessageBox (Message Dialog) In fact, he did not have any good introduction, only use of its message status to return to execute other code and normal message prompts display.

1. Processing code with message result return

C # code
    1. DialogResult resultnum=messagebox.show (The database file has been backed up to "+ Cls_dbbackrootpath +"), do you want to open the backup directory? "Database backup succeeded", Messageboxbuttons.yesno, MessageBoxIcon.Information);
    2. if (Resultnum = = Dialogresult.yes)//Decide whether to press "yes" button
    3. {
    4. Opendirectoryaddress (Cls_dbbackrootpath);
    5. }

There is no need to do the introduction, look at the message dialog box several parameters are what

2. message dialog box with different posture

C # code
    1. MessageBox.Show ("Here is the message content", "message tip title", the button displayed on the message dialog box, the hint icon displayed on the message dialog box);

Iii.DirectoryInfo (directory information) detect whether a directory exists, create a directory folder in the software is mainly used to analyze and create the specified file address string in all levels of the directory

1. Detect the existence of a directory using the Exists method

C # code
    1. DirectoryInfo curfolderroot = new DirectoryInfo (cls_dbrootpath);//Specify the physical address of the folder to be detected
    2. if (curfolderroot.exists)
    3. {
    4. //...
    5. }

2. Create the directory using the Create () method

C # code
    1. DirectoryInfo curfolderroot = new DirectoryInfo (cls_dbrootpath);//Specify the physical address of the folder to be detected
    2. if (curfolderroot.exists)
    3. {
    4. Curfolderroot.create ()
    5. }

Iv.FileInfo (file information) get file information, copy, delete files, etc., write the relevant information about the eligible files under the specified folder to the DataGridView control in turn.

1. Get the file Information code:

C # code
    1. FileInfo dbfile = new FileInfo (dbpath);
    2. Write to a column on a row of the DataGridView control
    3. Dgridefilelist.rows[rowsnum]. CELLS[1]. Value = Dbfile.length;
    4. Modify Time Write
    5. Dgridefilelist.rows[rowsnum]. CELLS[5]. Value = DbFile.LastWriteTime.ToString ();

2. Detect whether a file exists perform a delete copy operation

C # code
    1. FileInfo copyfile = new FileInfo (Copytopath);
    2. Detect if a file exists
    3. if (copyfile.exists)
    4. {
    5. Delete operation if there is a file
    6. File.delete (Copytopath);
    7. }
    8. Performing copy operations on files
    9. File.Copy (DBPath, Copytopath);

Five,DataGridView (data Table control) for display, update, delete, and so on the operation of the list of data

1. Adding data to a control that traverses the requirements

C # code
  1. Filestotelsize + = Curdbfile.length;
  2. To write file information to a string array
  3. string[] Fileinfoarr = new string[]{
  4. CurDbFile.FullName.Replace (Cls_dbrootpath, ""). ToString (),
  5. Checkfile.formatsize (Curdbfile.length),
  6. "0",
  7. "Not Compressed",
  8. Checkfile.gettypename (FilePath),
  9. CurDbFile.LastWriteTime.ToString ()
  10. };
  11. Adding file row array data to a control row set
  12. DGRIDEFILELIST.ROWS.ADD (Fileinfoarr);
  13. Refresh Control Display
  14. Dgridefilelist.refresh ();

2. Let the control scroll automatically with the vertical scrollbar

C # code
    1. Dgridefilelist.firstdisplayedscrollingrowindex = i;
    2. Dgridefilelist.refresh ();

3. Cursor positioning follow traversal Navigate to the control cell

C # code
    1. Dgridefilelist.currentcell=dgridefilelist.rows[i]. Cells[0];
    2. Dgridefilelist.refresh ();

4.DataRowView Delete Control Select row

C # code

    1. Delete selected row data
    2. if (This.dGrideFileList.SelectedRows.Count > 0)
    3. {
    4. DataRowView DRV = dgridefilelist.selectedrows[0]. Databounditem as DataRowView;
    5. Drv. Delete ();
    6. }

Vi.Process Startup Exporler.exe Open the specified physical Address folder

C # code
  1. #region Open Directory Address
  2. <summary>
  3. Open Directory Address
  4. </summary>
  5. <param name= "diraddress" > Need to open folder directory physical Address </param>
  6. private void Opendirectoryaddress (string diraddress)
  7. {
  8. DirectoryInfo dirfolder = new DirectoryInfo (diraddress);
  9. if (dirfolder.exists)
  10. {
  11. System.Diagnostics.Process.Start ("Explorer.exe", diraddress);
  12. }
  13. Else
  14. {
  15. MessageBox.Show ("No directory address needed to be opened", "Error hint", MessageBoxButtons.OK, Messageboxicon.error);
  16. }
  17. }
  18. #endregion


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.