Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. long path to short path

long path to short path

Scheduled Pinned Locked Moved C#
csharpdatabase
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elena12345
    wrote on last edited by
    #1

    HI, My program has to trigger a dumb application that won't take paths with spaces.:rolleyes: Do you know if there is a built in function in C# that translates long file paths to short file paths. Thank you, Elena

    H 1 Reply Last reply
    0
    • E elena12345

      HI, My program has to trigger a dumb application that won't take paths with spaces.:rolleyes: Do you know if there is a built in function in C# that translates long file paths to short file paths. Thank you, Elena

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      There is nothing in the .NET base class library that does this (at least nothing public that I can remember / find), but - if your app runs on Windows 2000 or higher (any NT-based system) - you can P/Invoke the function PathGetShortPath:

      [DllImport("shell32.dll", CharSet=CharSet.Unicode)]
      public static extern void PathGetShortPath(string path);

      You pass the long path into the method and after it returns path contains the short path. If you need something that works on all operating systems that .NET runs on, you'll have to enumerate each directory and the filename and change it to the 8.3 filename format. It's been a long time since I've worked with these, so you might want to check the PSDK to see if it documents how similarily named files or directories are resolved using the "~N" notation (like "MICROS~1", "MICROS~2", etc.). You should also trying googling for the solution. I'm sure someone else has had to deal with this before.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      S 1 Reply Last reply
      0
      • H Heath Stewart

        There is nothing in the .NET base class library that does this (at least nothing public that I can remember / find), but - if your app runs on Windows 2000 or higher (any NT-based system) - you can P/Invoke the function PathGetShortPath:

        [DllImport("shell32.dll", CharSet=CharSet.Unicode)]
        public static extern void PathGetShortPath(string path);

        You pass the long path into the method and after it returns path contains the short path. If you need something that works on all operating systems that .NET runs on, you'll have to enumerate each directory and the filename and change it to the 8.3 filename format. It's been a long time since I've worked with these, so you might want to check the PSDK to see if it documents how similarily named files or directories are resolved using the "~N" notation (like "MICROS~1", "MICROS~2", etc.). You should also trying googling for the solution. I'm sure someone else has had to deal with this before.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        S Offline
        S Offline
        Stephane Rodriguez
        wrote on last edited by
        #3

        Does not work on NT. It's only for 2000 and higher. This API function call works on both OSes : ["kernel32.dll"] GetShortPathName(string szLongPath, string szShortPath, long dwLength).


        RSS feed

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups