Link to home
Start Free TrialLog in
Avatar of lvnv
lvnv

asked on

GetShortPathName

I would like to make "f.FileName" and "f.SelectedPath"  below the short paths, but I'm not sure what I need to declare...I keep getting errors beyond the syntax of my GetShortPathName(xx,xxx,xx)
so I figured I'd just let someone get the points...


Here's the current code that I want to change to shortpath:

        private void buttonBrowseFiles_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog f = new OpenFileDialog())
            {
                f.FileName = this.textBoxBackupSelection.Text;
                f.Multiselect = false;

                if (f.ShowDialog() == DialogResult.OK)
                {
                    this.textBoxBackupSelection.Text = f.FileName;
                }
            }
        }

        private void buttonBrowseFolders_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog f = new FolderBrowserDialog())
            {
                f.ShowNewFolderButton = false;
                f.RootFolder = Environment.SpecialFolder.MyComputer;

                f.SelectedPath = this.textBoxBackupSelection.Text;

                if (f.ShowDialog() == DialogResult.OK)
                {
                    this.textBoxBackupSelection.Text = f.SelectedPath;
                }
            }
        }
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

What do you mean by short path?

Avatar of lvnv
lvnv

ASKER

GetShortPathName Function  ... it's a M$ thing...
ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lvnv

ASKER

graye - REALLY COOL SITE ... I will definitely use that more in the future!

it should be called more than anyone ever needed to know about p/invokes...

but where do I put this?

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



just put it inside a class somewhere, and then call it like any other part of the language/framework, such as

StringBuilder sb = new Stringbuilder(12);
GetShortPathName("c:\\somelongfilename.text", sb);
Avatar of lvnv

ASKER

I get this error:
"the type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)

<aside: the grammar seems weird in the error, but that's what it says>


I sure thought this would be easier than it has turned out...

GRRR....
Avatar of lvnv

ASKER

using System.Runtime.InteropServices;

needs to be at the top...



I sure with that there was a definitive guide to this.  My style of learning just doesn't seem to agree with the M$ documentation...


Avatar of lvnv

ASKER

that's really a good site!