Solved

GetShortPathName

Posted on 2007-11-14
8
751 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

13 Experts available now in Live!

Get 1:1 Help Now