浅谈VBA和.NET中文件名信息的处理
来源:互联网 发布:网络营销策划培训 编辑:程序博客网 时间:2024/05/27 06:16
在程序中操作文件名是我们经常遇到的事情。使用VBA时有很少的内置功能能帮助你。当使用VB.Net我认为我需要做同样的事情,就像我一直在用VBA做的。直到最近,我发现很多的.Net框架集的实用工具可以使工作更容易。
.Net框架集是使用任何.Net语言编程序的巨大优势之一。相比之下,VBA具有非常少的内置功能。可能你们当中的人和我一样仍然在探寻它的功能,所以我会把我认为.Net中有用的东西介绍到我的博客(Mod the Machine)中来。
在使用VBA的过程中,我写了一些和文件名有关的函数。使用这些我可以提取一个文件名的信息和把它们组织起来产生新的文件名。这些函数如下:
' 返回的输入文件名的路径。
Public Function FilePath(ByVal fullFilename As String) As String
' Extract the path by getting everything up to and
' including the last backslash "/".
FilePath = Left$(fullFilename, InStrRev(fullFilename, "/") - 1)
End Function
' 返回文件的名称,没有路径。
Public Function Filename(ByVal fullFilename As String) As String
' Extract the filename by getting everything to
' the right of the last backslash.
Filename = Right$(fullFilename, Len(fullFilename) – _
InStrRev(fullFilename, "/"))
End Function
' 返回输入文件名的基本名称,
' 无路径或扩展名。
Public Function BaseFilename(ByVal fullFilename As String) As String
' Extract the filename by getting everttgubg to
' the right of the last backslash.
Dim temp As String
temp = Right$(fullFilename, Len(fullFilename) – _
InStrRev(fullFilename, "/"))
' Get the base filename by getting everything to
' the left of the last period ".".
BaseFilename = Left$(temp, InStrRev(temp, ".") - 1)
End Function
' 返回的输入文件扩展名。
Public Function FileExtension(ByVal fullFilename As String) As String
' Extract the filename by getting everthing to
' the right of the last backslash.
Dim temp As String
temp = Right$(fullFilename, Len(fullFilename) – _
InStrRev(fullFilename, "/"))
' Get the base filename by getting everything to
' the right of the last period ".".
FileExtension = Right$(temp, Len(temp) - InStrRev(temp, ".") + 1)
End Function
' 测试文件名的函数。
Public Sub TestFileFunctionsVBA()
Dim fullFilename As String
fullFilename = "C:/Samples/Models/Parts/OilPan/OilPan.ipt"
Debug.Print FilePath(fullFilename)
Debug.Print Filename(fullFilename)
Debug.Print BaseFilename(fullFilename)
Debug.Print FileExtension(fullFilename)
End Sub
以下是在立即窗口中看到的结果。
C:/Samples/Models/Parts/OilPan
OilPan.ipt
OilPan
.ipt
使用VB.Net将容易得多。.Net框架集支持的IO类具有与文件与文件路径相关功能。IO类提供访问路径的类。如下代码是用VB.Net的功能来做上面的VBA代码所做的相同的事情,但它使用.NET框架集的功能,而不是自定义函数。您将需要导入System.IO Imports命名空间,或直接指定命名空间的完整路径来调用,如GetDirectoryName,就向下面代码显示的:
Imports System.IO
' 测试.Net框架集的路径功能。
Public Sub TestFileFunctionsVBNet()
Dim fullFilename As String
fullFilename = "C:/Samples/Models/Parts/OilPan/OilPan.ipt"
Debug.Print(System.IO.Path.GetDirectoryName(fullFilename))
Debug.Print(Path.GetFileName(fullFilename))
Debug.Print(Path.GetFileNameWithoutExtension(fullFilename))
Debug.Print(Path.GetExtension(fullFilename))
End Sub
正如你可以看到的,结果是一样的,但是不必象用VBA一样编写自定义函数。
C:/Samples/Models/Parts/OilPan
OilPan.ipt
OilPan
.ipt
- 浅谈VBA和.NET中文件名信息的处理
- matlab中自定义文件名和系统自带文件名重复的处理
- VBA中常用的字符串处理函数
- Linux中 vim的文件名处理函数
- 处理Selection对象和Range对象——Word VBA中重要的两个对象
- 处理Selection对象和Range对象——Word VBA中重要的两个对象
- 处理Selection对象和Range对象——Word VBA中重要的两个对象
- .Net 中 处理长文件名或者路径名
- 关于路径和文件名的处理
- 浅谈.NET中可用的定时器和计时器【上篇】
- 浅谈.NET中接口和类的区别
- 浅谈在线考试系统中有关人员信息的设计处理建议
- 【VBA研究】VBA从字符串中取出数据信息的函数
- VBA的异常处理
- JavaWeb开发中,文件下载的时候,ie和firefox文件名的处理
- 照片采集信息 Excel表单处理 VBA
- ASP.NET实现下载功能的中文文件名乱码处理
- ASP.NET实现下载功能的中文文件名乱码处理
- ArcSDE版本的理解(转)
- 供应不锈钢门花设备
- 谷歌加快发展电子商务业务 已推产品评论功能
- jquery回调函数接受服务器端返回的数据
- 看ExtJs API文档的阅读方法
- 浅谈VBA和.NET中文件名信息的处理
- cobol中常用的数据类型
- IE6下png图片和png背景透明
- wince添加Hive-based Registry
- [转]Google maps图像分割方法探索与分析
- C#异步调用的几种方式介绍
- Flex与后台交互的4种方法
- 魔兽世界场景组织浅析
- WebService创建、发布、及调用