Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compress backup C#

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to compress backup CVM, which is very detailed and has certain reference value. Friends who are interested must finish reading it!

Compressed backup C#

Although there is source code control, but in line with the principle that all important computer files should be backed up, we still need to back up the program as a whole from time to time. General program backup is to copy and package the entire program directory. There may be a lot of junk files, and it is more troublesome to operate by hand, so we programmers think of making up a Mini Program to back up the program. In order to facilitate the use of this program can also be linked to the integrated development environment, easy to call at any time.

Generally speaking, we use VS.NET as the development environment, so this Mini Program will become an extension of VS.NET. However, it is not very convenient to write an extension for VS.NET, so we think of a more convenient way to extend VS.NET, which is VBA.NET.

VBA.NET is a way to extend VS.NET and is a continuation of Office's VBA in VS.NET. It is easy to use, closely integrated with VS.NET, easy to explain and run, easy to debug, and the functions can be bound to the toolbar and menu of VS.NET, so it is easy to call.

Now let's talk about the process of compressing backup C# project. Here is an example of VS.NET2003, and the code in this article can only deal with VS.NET2003 's C# project. Open a VS.NET2003 C# project file (extension .csproj) with notepad, and you can see that this is a XML file, but this XML file does not have a XML declaration header, but its encoding format is GB2312. When the .NET Framework loads a XML file, if the XML declaration header is not found, the default encoding format is UTF8, so you cannot directly use System.XML.XmlDocument.Load to load the file. In this program, you will use the GB2312 encoding format (the code in .NET is 936) to read all the text contents of the C# project file as a text file, and then use System.Xml.XmlDocument.LoadXml to load the XML document.

In the XML document of a C # project, starting from the root node, the path VisualStudioProject/CSHARP/Build/Referencds/Reference is the reference used by the project, that is, the file name of the DLL used. The path VisualStudioProject/CSHARP/Files/Include/File lists all the files included in the project. The program will use these two information to obtain the files to be copied. At this point, the copy of the program is a clean C# project, and other files contained in the C# project directory will not be copied.

After the program copies the file to a temporary directory, it calls the command line program of WinRar to compress the project file. In this way, the compressed backup C# project is completed.

Click VS.NET menu item "tools-> Macro-> Macro IDE", open VS.NET 's VBA.NET integrated development environment, write code, then switch to VS.NET 's IDE, right-click the pop-up shortcut menu on the sidebar, select the bottom Custom menu item, switch to the commands page, select Macro in the list on the left, and select the function of VBA.NET just written in the list on the right. Then drag it to the VS.NET toolbar to bind the toolbar button to the VBA.NET function, and then you can only click this button to compress and back up your current edited C# project, which is so convenient. The following is a demonstration video of the operation.

The complete VBA.NET source code is

Public Sub CreateCSProjectRAR ()

If CheckCSProject () = False Then

Return

End If

Dim strPath As String

Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project

StrPath = System.IO.Path.GetFileNameWithoutExtension (myPrj.FullName)

Dim strFileName As String

/ / set the directory to save the generated files

StrPath = System.IO.Path.Combine

("D:\ SourceBack", strPath & "[" & System.DateTime.Now.ToString ("yyyy-MM-dd") & "]")

StrFileName = strPath & ".rar"

If System.IO.File.Exists (strFileName) Then

System.IO.File.Delete (strFileName)

End If

Dim iCount As Integer = CopyCSProjectFiles (strPath)

If System.IO.File.Exists (strFileName) Then

System.IO.File.Delete (strFileName)

End If

If iCount > 0 Then

DTE.StatusBar.Text = "generating compressed file"

Dim start As New System.Diagnostics.ProcessStartInfo

/ / specify the executable file name of the WinRar compression software here, and modify this file name if WinRar is installed in another directory

Start.FileName = "C:\ Program Files\ WinRAR\ WinRAR.exe"

Start.Arguments = "a-r-df-ep1" & strFileName & "& strPath

Dim p As SystemSystem.Diagnostics.Process = System.Diagnostics.Process.Start (start)

P.WaitForExit ()

DTE.StatusBar.Text = "compressed file generated" & strFileName

MsgBox ("compressed files generated" & strFileName, MsgBoxStyle.Information, "system prompt")

End If

End Sub

/ / copy the C# project of the currently edited VS.NET2003 to the directory specified by the user. VS.NET2005 is not supported.

Public Sub CopyCSProject ()

/ / check whether it is a C# project

If CheckCSProject () = False Then

Return

End If

/ / Let the user enter the directory

Dim strPath As String = InputBox ("Please enter the output directory name", "input")

If strPath Is Nothing Then

Return

End If

If strPath.Length = 0 Then

Return

End If

/ / copy the file

Dim iCount As Integer = CopyCSProjectFiles (strPath)

MsgBox ("co-copy" & iCount & "a file")

End Sub

/ / copy all the included files of the current VS.NET2003 C# project to the specified directory. VS.NET2005 is not supported.

/ / do not copy files referenced by absolute paths in the project

Public Function CopyCSProjectFiles (ByVal strPath As String) As Integer

If CheckCSProject () = False Then

Return-1

End If

If System.IO.Directory.Exists (strPath) = False Then

System.IO.Directory.CreateDirectory (strPath)

End If

Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project

/ / load the project file

Dim myFile As New System.IO.StreamReader

(myPrj.FullName, System.Text.Encoding.GetEncoding)

Dim myDoc As New System.Xml.XmlDocument

MyDoc.LoadXml (myFile.ReadToEnd ())

MyFile.Close ()

Dim ThisPath As String = System.IO.Path.GetDirectoryName (myPrj.FullName)

/ / copy the project definition file itself

CopyFile (myPrj.FullName, strPath)

Dim FileCount As Integer

Dim myElement As System.Xml.XmlElement

Dim strFileName As String

Dim strNewFileName As String

/ / copy referenced files

For Each myElement In myDoc.SelectNodes

("VisualStudioProject/CSHARP/Build/Referencds/Reference")

StrFileName = myElement.GetAttribute ("HintPath")

If System.IO.Path.IsPathRooted (strFileName) = False Then

CopyFile (ThisPath, strPath, strFileName)

FileCountFileCount = FileCount + 1

End If

Next

/ / copy the project file

For Each myElement In myDoc.SelectNodes

("VisualStudioProject/CSHARP/Files/Include/File")

StrFileName = myElement.GetAttribute ("RelPath")

If Not strFileName Is Nothing Then

If System.IO.Path.IsPathRooted (strFileName) = False Then

CopyFile (ThisPath, strPath, strFileName)

FileCountFileCount = FileCount + 1

DTE.StatusBar.Text = FileCount & "copying files" & strFileName

End If

End If

Next

Return FileCount

End Function

/ / check whether the currently edited project is a C# project

Public Function CheckCSProject () As Boolean

Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project

If UCase (System.IO.Path.GetExtension (myPrj.FullName)) ".CSPROJ" Then

MsgBox ("current project is not a C# project", MsgBoxStyle.Information, "system hint")

Return False

End If

Return True

End Function

/ / create the specified directory

Public Sub CreateDirectory (ByVal strDir As String)

If System.IO.Directory.Exists (strDir) = False Then

System.IO.Directory.CreateDirectory (strDir)

End If

End Sub

/ / copy the files with the specified relative path under the specified directory to another directory, leaving the relative path unchanged

Public Sub CopyFile (ByVal strPath2 As String

ByVal strPath3 As String, ByVal strFilePath As String)

Dim strName1 As String = System.IO.Path.Combine (strPath2, strFilePath)

Dim strName2 As String = System.IO.Path.Combine (strPath3, strFilePath)

Dim dir1 As String = System.IO.Path.GetDirectoryName (strName1)

If System.IO.Directory.Exists (dir1) = False Then

System.IO.Directory.CreateDirectory (dir1)

End If

Dim dir2 As String = System.IO.Path.GetDirectoryName (strName2)

If System.IO.Directory.Exists (dir2) = False Then

System.IO.Directory.CreateDirectory (dir2)

End If

System.IO.File.Copy (strName1, strName2, True)

End Sub

/ / copy the specified file to the specified directory

Public Sub CopyFile (ByVal strFileName As String, ByVal strNewPath As String)

System.IO.File.Copy (strFileName, System.IO.Path.Combine

(strNewPath, System.IO.Path.GetFileName (strFileName)), True)

End Sub

The above is all the contents of the article "how to compress backup C#". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report