Solved

GetShortPathName

Posted on 2007-11-14
8
752 Views
Last Modified: 2013-12-17
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;
                }
            }
        }
0
Comment
Question by:lvnv
  • 5
  • 2
8 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 20282592
What do you mean by short path?

0
 

Author Comment

by:lvnv
ID: 20282911
GetShortPathName Function  ... it's a M$ thing...
0
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
ID: 20282934
Not that we don't like points....  but there is a nice web site out there called pinvoke.net that has (almost all) of the Win32 API declarations already written for you!

http://www.pinvoke.net/default.aspx/kernel32/GetShortPathName.html

0
 

Author Comment

by:lvnv
ID: 20284184
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);



0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 41

Expert Comment

by:graye
ID: 20285178
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);
0
 

Author Comment

by:lvnv
ID: 20285293
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....
0
 

Author Comment

by:lvnv
ID: 20285609
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...


0
 

Author Comment

by:lvnv
ID: 20285925
that's really a good site!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

864 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now