此贴用于收录各种在实际项目中需要用到的语法,持续更新中
1.查询一个进程是否开启
Dim p() As System.Diagnostics.Process
p = Process.GetProcessesByName("Excel")
console.WriteLine(p.GetType)
If p.Count > 0 Then
console.WriteLine("Process is Running")
Else
console.WriteLine("Process isn't Running")
end if
2.打开一个进程
参考链接:https://bbs.csdn.net/topics/392555533
system.Diagnostics.Process.Start("Excel.exe")
3.删除某个文件
Dim fileExist As Boolean
fileExist = system.IO.file.Exists("XXX.txt")
If fileExist = True Then
system.IO.file.Delete("XXX.txt")
End If
4.删除文件夹
Dim fileExist As Boolean
fileExist = system.IO.Directory.Exists("XXX")
If fileExist = True Then
'当此文件夹下有文件夹或者文件,需要加第二个参数True,表示把里面所有的文件及文件夹一并删除
'如果此文件夹里没有任何内容,可以直接写 system.IO.Directory.Delete("XXX")
system.IO.Directory.Delete("XXX",True)
End If
5.检测目录下文件距离现在是否大于7日,如果是就删除
Dim sAllFile As String()
Dim sSubFile As String
Dim FileObj As System.IO.FileInfo
sAllFile = system.IO.Directory.GetFiles(XXX)
For Each sSubFile In sAllFile
FileObj = New System.IO.FileInfo(sSubFile)
If (System.DateTime.Now - FileObj.CreationTime).Days > 7 Then
system.IO.file.Delete(sSubFile)
End If
Next sSubFile
6.获取Excel最大行
rowCount = Sheet.Dimension.Rows
7.获取Excel1列里面所有值
For Each cell As ExcelRangeBase In sht.Cells("A1:A1048576").Where(Function(c) Not String.IsNullOrWhiteSpace(c.Value.ToString))
console.WriteLine(cell.Value.ToString)
Next