Displaypath() Method

Displaypath() Method

<p>Visual FoxPro Toolkit for .NET DisplayPath() Method</p><p>Receives a filename with path and maxlength as parameters and returns a truncated version of the path for display purposes </p><p>[Visual Basic] Public Shared Function DisplayPath(ByVal cFileNameWithPath As String, ByVal nMaxLength As Integer) As String</p><p>[C#] public static string DisplayPath(string cFileNameWithPath, int nMaxLength)</p><p>Example</p><p>[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</p><p>Implementation</p><p>[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</p><p>'Check if we are within the max length then return the whole string If cFileNameWithPath.Length <= nMaxLength Then Return cFileNameWithPath End If</p><p>'Split the string into an array using the \ as a delimiter Dim cSepatator() As Char = {"\"c}</p><p>Dim aStr() As String aStr = cFileNameWithPath.Split(cSepatator)</p><p>'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</p><p>Dim s As String = "" Dim n As Integer = 0</p><p>'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</p><p>'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</p><p>'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</p><p>'Add the header if the bool is true If lAddHeader = True Then lcRetVal = lcBegin + lcRetVal End If</p><p>'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</p><p>Return lcRetVal End Function</p><p>[C#] public static string DisplayPath(string cFileNameWithPath, int nMaxLength) { //We will begin by taking the string and splitting it apart into an array</p><p>//Check if we are within the max length then return the whole string if (cFileNameWithPath.Length <= nMaxLength) { return cFileNameWithPath; }</p><p>//Split the string into an array using the \ as a delimiter char[] cSeparator = {'\\'}; string[] aStr; aStr = cFileNameWithPath.Split(cSeparator);</p><p>//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;</p><p> string s = ""; int n = 0;</p><p>//Now we loop backwards through the string int i = 0; for (i = aStr.Length - 1 ; i > 0; i--) { s = '\\' + aStr[i]; n = s.Length;</p><p>//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; }</p><p>//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; } }</p><p>//Add the header if the bool is true if (lAddHeader == true) { lcRetVal = lcBegin + lcRetVal; }</p><p>//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); }</p><p> return lcRetVal; }</p><p>Requirements</p><p>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)</p><p>See Also VFPToolkit.files Members | VFPToolkit Namespace </p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us