Previous topicNext topic
Help > 开发指南 > SanMuGrid平台编程 > 静态类 > Sys >
GetRelativePath

GetRelativePath创建从一个文件或文件夹到另一个文件或文件夹的相对路径。目录必须在同一个驱动器,比如都在D盘。

语法:

GetRelativePath(fromPath,toPath)

参数说明

名称 说明
fromPath 必填项,字符串类型,包含定义相对路径起点的目录。
toPath 必填项,字符串类型,包含定义相对路径端点的路径。

Vb.Net
'获得相同驱动盘的文件相对路径
Dim strPath As String=Sys.GetRelativePath("E:\Temp","E:\Temp\Log\DEBUG\20220501.log")
Proj.MsgDebug.Add("文件的相对路径:{0}",strPath)
'从相对路径再获得绝对路径
Dim strPathFull As String=Path.Combine("E:\Temp",strPath)
Proj.MsgDebug.Add("源目录与相对文件的组合:{0}",strPathFull)
strPathFull=Path.GetFullPath(strPathFull)
Proj.MsgDebug.Add("重新获得文件的绝对路径:{0}",strPathFull)
'获得目录的相对路径
strPath=Sys.GetRelativePath("E:\Temp","E:\Temp\Log\DEBUG\")
Proj.MsgDebug.Add("目录的相对路径:{0}",strPath)
'从相对路径再获得绝对路径
strPathFull=Path.Combine("E:\Temp",strPath)
Proj.MsgDebug.Add("源目录与相对目录的组合:{0}",strPathFull)
strPathFull=Path.GetFullPath(strPathFull)
Proj.MsgDebug.Add("重新获得目录的绝对路径:{0}",strPathFull)

'返回结果:文件的相对路径:.\Log\DEBUG\20220501.log
'返回结果:源目录与相对文件的组合:E:\Temp\.\Log\DEBUG\20220501.log
'返回结果:重新获得文件的绝对路径:E:\Temp\Log\DEBUG\20220501.log
'返回结果:目录的相对路径:.\Log\DEBUG\
'返回结果:源目录与相对目录的组合:E:\Temp\.\Log\DEBUG\
'返回结果:重新获得目录的绝对路径:E:\Temp\Log\DEBUG\

C#
// 获得相同驱动盘的文件相对路径
string strPath = Sys.GetRelativePath(@"E:\Temp", @"E:\Temp\Log\DEBUG\20220501.log");
Proj.MsgDebug.Add("文件的相对路径:{0}", strPath);
// 从相对路径再获得绝对路径
string strPathFull = Path.Combine(@"E:\Temp", strPath);
Proj.MsgDebug.Add("源目录与相对文件的组合:{0}", strPathFull);
strPathFull = Path.GetFullPath(strPathFull);
Proj.MsgDebug.Add("重新获得文件的绝对路径:{0}", strPathFull);
// 获得目录的相对路径
strPath = Sys.GetRelativePath(@"E:\Temp", @"E:\Temp\Log\DEBUG\");
Proj.MsgDebug.Add("目录的相对路径:{0}", strPath);
// 从相对路径再获得绝对路径
strPathFull = Path.Combine(@"E:\Temp", strPath);
Proj.MsgDebug.Add("源目录与相对目录的组合:{0}", strPathFull);
strPathFull = Path.GetFullPath(strPathFull);
Proj.MsgDebug.Add("重新获得目录的绝对路径:{0}", strPathFull);

//返回结果:文件的相对路径:.\Log\DEBUG\20220501.log
//返回结果:源目录与相对文件的组合:E:\Temp\.\Log\DEBUG\20220501.log
//返回结果:重新获得文件的绝对路径:E:\Temp\Log\DEBUG\20220501.log
//返回结果:目录的相对路径:.\Log\DEBUG\
//返回结果:源目录与相对目录的组合:E:\Temp\.\Log\DEBUG\
//返回结果:重新获得目录的绝对路径:E:\Temp\Log\DEBUG\