[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Convert from long path name to short path name

Posted on 2007-08-02
10
Medium Priority
?
826 Views
Last Modified: 2013-11-07
How do i convert a string from a long path name to a short path name in vs2005 c#?
0
Comment
Question by:jmf8883
  • 5
  • 4
10 Comments
 
LVL 6

Expert Comment

by:gjutras
ID: 19620460
do you mean joliet filesystem name to 8.3 name or filename without path?
0
 

Author Comment

by:jmf8883
ID: 19620519
joliet I guess I was something like this c:\sometihng\something\some.zip turned into
c:\some~1\some~1\thin~1.zip
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19620548
Try this:

using System;
using System.Runtime.InteropServices;
using System.Text;

public class PathConverter
{

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern uint GetShortPathName(
       [MarshalAs(UnmanagedType.LPTStr)]string lpszLongPath,
       [MarshalAs(UnmanagedType.LPTStr)]StringBuilder lpszShortPath,
       uint cchBuffer);

    /// <summary>
    /// The ToLongPathNameToShortPathName function retrieves the short path form of a specified long input path
    /// </summary>
    /// <param name="longName">The long name path</param>
    /// <returns>A short name path string</returns>
    public static string ToShortPathName(string longName)
    {
        StringBuilder shortNameBuffer = new StringBuilder(256);
        uint bufferSize = (uint)shortNameBuffer.Capacity;

        int result = (int)GetShortPathName(longName, shortNameBuffer, bufferSize);
        return shortNameBuffer.ToString();
    }

}

Example:
        string path = PathConverter.ToShortPathName(@"C:\Multimedia Files\Graphics\Web\Animations\Microsoft GIF Animator\Wheretoday.gif");

Bob
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:jmf8883
ID: 19621045
I've been playing with variations of that code for the past 2 hours...it doesn't seem to work
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19621459
What is happening?  What O/S are you working with?

Bob
0
 

Author Comment

by:jmf8883
ID: 19629693
windows xp short name buffer is coming back empty
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19634075
That is funny, because I have Windows XP Professional SP2, and that code works for me.

Bob
0
 

Author Comment

by:jmf8883
ID: 19635822
I just tried it on my home computer and this code does not work:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace pathnametest
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern uint GetShortPathName(
       [MarshalAs(UnmanagedType.LPTStr)]string lpszLongPath,
       [MarshalAs(UnmanagedType.LPTStr)]StringBuilder lpszShortPath,
       uint cchBuffer);

        public Form1()
        {
            InitializeComponent();
        }

        public static string ToShortPathName(string longName)
        {
            StringBuilder shortNameBuffer = new StringBuilder(256);
            uint bufferSize = (uint)shortNameBuffer.Capacity;

            int result = (int)GetShortPathName(longName, shortNameBuffer, bufferSize);
            return shortNameBuffer.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = ToShortPathName(@"C:\Multimedia Files\Graphics\Web\Animations\Microsoft GIF Animator\Wheretoday.gif");
        }
    }
}
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 19637276
"Does not work" doesn't help much.  Do you have that file (is it a valid path)?

Bob
0
 

Author Comment

by:jmf8883
ID: 19637560
oh...I feel like an idiot.  I was thinking it would work on any string.  Forgot that those path names were relative to the other files and folders.  Sorry.

Jonathan
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 19 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question