如何在 Vb.Net 应用程序中创建自动备份文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20770335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:15:06  来源:igfitidea点击:

How To Create Automated Backup Files In Vb.Net Application?

vb.net

提问by user2994853

How to create automated backup files and backup type compress. But I want to backup the folder containing Data Files (.doc,.xls,.tiff,.pdf...) but not sql database. I Want file Backup SHARE-AC.zip. But now have get folder SHARE-AC don't zip.

如何创建自动备份文件和备份类型压缩。但我想备份包含数据文件(.doc、.xls、.tiff、.pdf...)但不是 sql 数据库的文件夹。我要文件备份SHARE-AC.zip。但现在有得到文件夹 SHARE-AC 不要压缩。

Imports System.IO
Imports System.IO.Compression
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim dstr As String
        Dim mstr As String
        Dim ystr As String
        Dim folstr As String
        Dim dsumstr As String

        dstr = DateTime.Today.ToString("dd")
        mstr = DateTime.Today.ToString("MM")
        ystr = DateTime.Today.ToString("yyyy")
        dsumstr = ystr & "-" & mstr & "-" & dstr
        folstr = "Y:\server1\Fileserver-" & dsumstr
        Try
            My.Computer.FileSystem.CreateDirectory(folstr)

            My.Computer.FileSystem.CreateDirectory(folstr & "\SHARE-AC")
            My.Computer.FileSystem.CopyDirectory("D:\SHARE-AC", folstr & "\SHARE-AC")



            Label1.Text = "Back up DATE   " & dsumstr & "  Complete"
        Catch ex As Exception
            Label1.Text = (ex.Message)
        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
End Class

回答by Pouya Samie

i dont get your question but If you just wanna zip your file you can use this

我不明白你的问题,但如果你只是想压缩你的文件,你可以使用这个

Update: You could use GZipStream in the System.IO.Compression namespace

更新:您可以在 System.IO.Compression 命名空间中使用 GZipStream

.NET 2.0.

.NET 2.0。

Public Shared Sub CompressFile(path As String)
Dim sourceFile As FileStream = File.OpenRead(path)
Dim destinationFile As FileStream = File.Create(path & ".gz")

Dim buffer As Byte() = New Byte(sourceFile.Length - 1) {}
sourceFile.Read(buffer, 0, buffer.Length)

Using output As New GZipStream(destinationFile, CompressionMode.Compress)
    Console.WriteLine("Compressing {0} to {1}.",sourceFile.Name,destinationFile.Name, False)

    output.Write(buffer, 0, buffer.Length)
End Using

' Close the files.
sourceFile.Close()
destinationFile.Close()
 End Sub 

.NET 4

.NET 4

Public Shared Sub Compress(fi As FileInfo)
' Get the stream of the source file.
Using inFile As FileStream = fi.OpenRead()
    ' Prevent compressing hidden and 
    ' already compressed files.
    If (File.GetAttributes(fi.FullName) And FileAttributes.Hidden) <> FileAttributes.Hidden And fi.Extension <> ".gz" Then
        ' Create the compressed file.
        Using outFile As FileStream = File.Create(Convert.ToString(fi.FullName) & ".gz")
            Using Compress As New GZipStream(outFile, CompressionMode.Compress)
                ' Copy the source file into 
                ' the compression stream.
                inFile.CopyTo(Compress)

                Console.WriteLine("Compressed {0} from {1} to {2} bytes.", fi.Name, fi.Length.ToString(), outFile.Length.ToString())
            End Using
        End Using
    End If
End Using
End Sub

回答by Al-3sli

if you want to zip the file on the directory you set use this code :

如果要将文件压缩到您设置的目录中,请使用以下代码:

    Dim dstr As String
    Dim mstr As String
    Dim ystr As String
    Dim folstr As String
    Dim dsumstr As String

    dstr = DateTime.Today.ToString("dd")
    mstr = DateTime.Today.ToString("MM")
    ystr = DateTime.Today.ToString("yyyy")
    dsumstr = ystr & "-" & mstr & "-" & dstr
    folstr = "Y:\server1\Fileserver-" & dsumstr
    Try
        My.Computer.FileSystem.CreateDirectory(folstr)

        My.Computer.FileSystem.CreateDirectory(folstr & "\SHARE-AC")
        ZipFile.CreateFromDirectory("D:\SHARE-AC", folstr & "\SHARE-AC\myfile.zip") ' Zip all file in your directory
        'My.Computer.FileSystem.CopyDirectory("D:\SHARE-AC", folstr & "\SHARE-AC")



        Label1.Text = "Back up DATE   " & dsumstr & "  Complete"
    Catch ex As Exception
        Label1.Text = (ex.Message)
    End Try

End Sub

YOU NEED TO IMPORT:

您需要导入:

 Imports System.IO
 Imports System.IO.Compression

UPDATE

更新

You can use SharpZipLipAnd here some Examples.

您可以使用SharpZipLip和这里的一些示例