我想处理添加到现有文件夹的任何新文件,并使用新文件名或目录名将其保存在文本文件中。
或多或少是这样的:
如果( 文件夹中有新文件“blah”),则将新文件路径附加到textfile.txt
所以我需要新的文件路径 感谢
答案 0 :(得分:0)
我可以建议采用here方法吗?这非常详尽,并解决了FileSystemWatcher类中的一些缺陷。
答案 1 :(得分:0)
以下是您尝试做的一个示例。您需要执行以下操作来测试它。 1.创建一个Windows窗体 2.添加一个按钮并将其命名为btnStart 3.添加一个按钮并将其命名为btnStop 4.添加一个文本框并将其命名为txtFolderToMonitor 5.添加一个文本框并将其命名为txtFolderToMoveTo 6.添加一个名为Label1的标签和laeve 7.添加一个文本框并将其命名为TextBox1
然后将以下内容粘贴为表单后面的代码。
Public Class Form1
Private _FilePath As String = String.Empty
Private _IsProcessing As Boolean = False
Private _sbJustAPlaceToHoldFileText As New System.Text.StringBuilder
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
Me.btnStop.Enabled = True
Me.btnStart.Enabled = False
Me.Label1.Visible = True
Me.TextBox1.Text = String.Empty
_FilePath = Me.txtFolderToMonitor.Text
_IsProcessing = True
Dim t As New System.Threading.Thread(AddressOf ProcessFoldersContents)
t.Start()
End Sub
Private Sub ProcessFoldersContents()
While Not Me._IsProcessing = False
Dim sfiles() As String
sfiles = System.IO.Directory.GetFiles(Me._FilePath)
For Each f As String In sfiles
ProcessFile(f)
Next
System.Threading.Thread.Sleep(5000)
End While
End Sub
Private Sub ProcessFile(f As String)
Dim strFileContents As String
Dim sJustFileName As String = System.IO.Path.GetFileNameWithoutExtension(f)
strFileContents = System.IO.File.ReadAllText(f)
Me._sbJustAPlaceToHoldFileText.Append(strFileContents)
System.IO.File.Move(f, Me.txtFolderToMoveTo.Text & "\" & sJustFileName)
End Sub
Private Sub btnStop_Click(sender As System.Object, e As System.EventArgs) Handles btnStop.Click
Try
Me._IsProcessing = False
Me.TextBox1.Text = Me._sbJustAPlaceToHoldFileText.ToString
Catch ex As Exception
Finally
Me.btnStart.Enabled = True
Me.btnStop.Enabled = False
Me.Label1.Visible = False
End Try
End Sub
结束班