自编小工具之一
说明:这些自编小工具是本人在学习开发和管理期间,同时为了工作需要而自行编写的小工具。将它们贴上blog作为学习或工作过程的记录。
使用dot Net开发Web项目,当项目要部署或发布时,为了施行软件保护----防止后台代码被查看,项目或解决方案被修改、盗用。则需要将后台代码、项目文件、解决方案文件等一切无关最终用户使用的文件统一删除。
为了实施这一保护,可使用配置工具,例如VSS。如果不想应用这些工具,也可以自己做一个简单的工具来实现这种删除备份功能。
功能描述:备份整个项目内容、删除某些需要保护的文件
程序描述:选择某个项目目录,获取该目录路径并在该目录的根目录下创建bak文件夹存放整个项目内容。
遍利整个项目目录,边备份边删除指定类型的文件。
源代码:
Public
Class Form1
Class Form1
Inherits System.Windows.Forms.Form
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Public Sub New()Sub New()
MyBase.New()
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'在 InitializeComponent() 调用之后添加任何初始化
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'窗体重写 dispose 以清理组件列表。
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents lbl As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents FolderShow As System.Windows.Forms.FolderBrowserDialog
Private dp As String
Private copyCnt(1) As Integer
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.lbl = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label2 = New System.Windows.Forms.Label
Me.FolderShow = New System.Windows.Forms.FolderBrowserDialog
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(536, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(104, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "选择项目根目录"
'
'lbl
'
Me.lbl.Location = New System.Drawing.Point(80, 16)
Me.lbl.Name = "lbl"
Me.lbl.Size = New System.Drawing.Size(440, 16)
Me.lbl.TabIndex = 1
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(72, 16)
Me.Label1.TabIndex = 2
Me.Label1.Text = "选择路径:"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(8, 72)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(632, 208)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = ""
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(8, 48)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(128, 16)
Me.Label2.TabIndex = 4
Me.Label2.Text = "已经被删除的文件:"
'
'FolderShow
'
Me.FolderShow.ShowNewFolderButton = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(648, 285)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.lbl)
Me.Controls.Add(Me.Button1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form1"
Me.Text = "项目源文件处理工具"
Me.ResumeLayout(False)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
#End Region
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
lbl.Text = ""
TextBox1.Text = ""
dp = ""
copyCnt(0) = 0
copyCnt(1) = 0
FolderShow.Reset()
FolderShow.ShowNewFolderButton = False
FolderShow.ShowDialog()
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
lbl.Text = FolderShow.SelectedPath.ToString
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
If lbl.Text.Length > 0 Then'组合bak文件夹
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
dp = lbl.Text.Substring(0, 3) & "bak" & lbl.Text.Substring(3, lbl.Text.Length - 3)
Try
FileSystem.MkDir(dp)
Catch ex As Exception
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Try
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Dim oCols As New Collection
Dim i As Integer
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
FindTaget(FolderShow.SelectedPath.ToString, oCols)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
For i = 1 To oCols.Count
TextBox1.Text = TextBox1.Text & oCols.Item(i) & vbTab
Next
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
TextBox1.Text = TextBox1.Text & vbCrLf & "共删除文件个数:" & oCols.Count.ToString
TextBox1.Text = TextBox1.Text & vbCrLf & "共拷贝文件个数:" & copyCnt(1).ToString
TextBox1.Text = TextBox1.Text & vbCrLf & "共拷贝文件夹个数:" & copyCnt(0).ToString
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End If
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'遍历列出所有目录
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Private Function FindTaget()Function FindTaget(ByVal sTmp As String, ByVal list As Collection)
Dim myPath, myName As String
Dim j, i As Integer
Dim sPath() As String
Dim sStr() As String
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
myPath = sTmp
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
If myPath.Substring(myPath.Length - 1, 1) <> "" Then
myPath = myPath & ""
End If
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
myName = Dir(myPath, FileAttribute.Directory) ' 找寻第一项。
j = 0
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
While Len(myName) > 0
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
If myName <> "." And myName <> ".." Then
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
' 使用位比较来确定 MyName 代表一目录。
If (GetAttr(myPath & myName) And FileAttribute.Directory) = FileAttribute.Directory Then
ReDim Preserve sPath(j)
sPath(j) = myName
j = j + 1 '目录计数器加1
'拷贝文件夹
Clone(myPath, myName, "dir")
Else
'先做文件拷贝
Clone(myPath, myName, "file")
'判断是否删除
sStr = Split(myName, ".")
If LCase(sStr(sStr.Length - 1)) = "vb" Or LCase(sStr(sStr.Length - 1)) = "cs" Or LCase(sStr(sStr.Length - 1)) = "vbproj" Then
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
list.Add(myName) '记录被
FileSystem.Kill(myPath & myName) '删除指定后缀名的文件
End If
End If
End If
myName = Dir()
End While
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
If j > 0 Then '递归遍历子目录
For i = 0 To UBound(sPath)
Call FindTaget(myPath & sPath(i) & "", list)
Next
End If
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Erase sPath
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Function
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Private Function Clone()Function Clone(ByVal path As String, ByVal name As String, ByVal Ftype As String)
Dim substr, fullpath As String
Dim icnt As Integer
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
fullpath = path & name
icnt = dp.Length - 4
substr = fullpath.Substring(icnt, fullpath.Length - icnt)'获取项目目录中的子目录路径
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
If Ftype = "dir" Then
'如果是文件夹,则新建文件夹
Try
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
FileSystem.MkDir(dp & substr)
copyCnt(0) = copyCnt(0) + 1
Catch ex As Exception
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Try
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ElseIf Ftype = "file" Then
'否则,复制文件
Try
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
FileSystem.FileCopy(fullpath, dp & substr)
copyCnt(1) = copyCnt(1) + 1
Catch ex As Exception
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Try
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End If
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Function
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Class
删除文件的时候加个判断,如果是只读文件,则将其属性重设。
If (GetAttr(pathName) And FileAttribute.ReadOnly) = FileAttribute.ReadOnly Then
FileSystem.SetAttr(pathName, FileAttribute.Normal)
End If