Displaypath() Method
Total Page:16
File Type:pdf, Size:1020Kb
Visual FoxPro Toolkit for .NET DisplayPath() Method
Receives a filename with path and maxlength as parameters and returns a truncated version of the path for display purposes
[Visual Basic] Public Shared Function DisplayPath(ByVal cFileNameWithPath As String, ByVal nMaxLength As Integer) As String
[C#] public static string DisplayPath(string cFileNameWithPath, int nMaxLength)
Example
[Visual Basic] Dim lcFile As String = "c:\My Folders\My Custom Folders\My Files\ResultFile.txt" DisplayPath(lcFile, 10) 'returns ResultFile DisplayPath(lcFile, 15) 'returns \ResultFile.txt DisplayPath(lcFile, 20) 'returns c:\...\ResultFile.txt DisplayPath(lcFile, 30) 'returns c:\...\My Files\ResultFile.txt [C#] string lcFile = @"c:\My Folders\My Custom Folders\My Files\ResultFile.txt" DisplayPath(lcFile, 10) //returns ResultFile DisplayPath(lcFile, 15) //returns \ResultFile.txt DisplayPath(lcFile, 20) //returns c:\...\ResultFile.txt DisplayPath(lcFile, 30) //returns c:\...\My Files\ResultFile.txt
Implementation
[Visual Basic] Public Shared Function DisplayPath(ByVal cFileNameWithPath As String, ByVal nMaxLength As Integer) As String 'We will begin by taking the string and splitting it apart into an array
'Check if we are within the max length then return the whole string If cFileNameWithPath.Length <= nMaxLength Then Return cFileNameWithPath End If
'Split the string into an array using the \ as a delimiter Dim cSepatator() As Char = {"\"c}
Dim aStr() As String aStr = cFileNameWithPath.Split(cSepatator)
'The first value of the array is taken in case we need to create the string Dim lcBegin As String = aStr(0) + "\..." Dim lnBeginLength As Integer = aStr(0).Length + 3 Dim lcRetVal As String = "" Dim lnLength As Integer = lcRetVal.Length Dim lAddHeader As Boolean = False
Dim s As String = "" Dim n As Integer = 0
'Now we loop backwards through the string Dim i As Integer = 0 For i = aStr.Length - 1 To 0 - 1 Step i - 1 s = "\"c + aStr(i) n = s.Length
'Check if adding the current item does not increase the length of the max string If lnLength + n <= nMaxLength Then 'In this case we can afford to add the item lcRetVal = s + lcRetVal lnLength += n Else Exit For End If
'Check if there is room to add the header and if so then reserve it by incrementing the length If (lAddHeader = False) And (lnLength + lnBeginLength <= nMaxLength) Then lAddHeader = True lnLength += lnBeginLength End If Next
'Add the header if the bool is true If lAddHeader = True Then lcRetVal = lcBegin + lcRetVal End If
'It is possible that the last value in the array itself was long. In such case simply use the substring of the last value If lcRetVal.Length = 0 Then lcRetVal = aStr(aStr.Length - 1).Substring(0, nMaxLength) End If
Return lcRetVal End Function
[C#] public static string DisplayPath(string cFileNameWithPath, int nMaxLength) { //We will begin by taking the string and splitting it apart into an array
//Check if we are within the max length then return the whole string if (cFileNameWithPath.Length <= nMaxLength) { return cFileNameWithPath; }
//Split the string into an array using the \ as a delimiter char[] cSeparator = {'\\'}; string[] aStr; aStr = cFileNameWithPath.Split(cSeparator);
//The first value of the array is taken in case we need to create the string string lcBegin = aStr[0] + "\\..."; int lnBeginLength = aStr[0].Length + 3; string lcRetVal = ""; int lnLength = lcRetVal.Length ; bool lAddHeader = false;
string s = ""; int n = 0;
//Now we loop backwards through the string int i = 0; for (i = aStr.Length - 1 ; i > 0; i--) { s = '\\' + aStr[i]; n = s.Length;
//Check if adding the current item does not increase the length of the max string if (lnLength + n <= nMaxLength) { //In this case we can afford to add the item lcRetVal = s + lcRetVal; lnLength += n; } else { break; }
//Check if there is room to add the header and if so then reserve it by incrementing the length if ((lAddHeader == false) && (lnLength + lnBeginLength <= nMaxLength)) { lAddHeader = true; lnLength += lnBeginLength; } }
//Add the header if the bool is true if (lAddHeader == true) { lcRetVal = lcBegin + lcRetVal; }
//It is possible that the last value in the array itself was long. In such case simply use the substring of the last value if (lcRetVal.Length == 0) { lcRetVal = aStr[aStr.Length-1].Substring(0,nMaxLength); }
return lcRetVal; }
Requirements
Namespace: VFPToolkit Class: VFPToolkit.files Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server family Assembly: VFPToolkit (in VFPToolkitNET.dll)
See Also VFPToolkit.files Members | VFPToolkit Namespace