By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,018 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,018 IT Pros & Developers. It's quick & easy.

PathYetAnotherMakeUniqueName or PathMakeUniqueName API call from C#

P: n/a
I'm having trouble getting the api PathYetAnotherMakeUniqueName to work.
Here is what I have so far. It is not working. What am i missing?

const int MAX_PATH = 260;

[DllImport("shell32.dll", EntryPoint="PathMakeUniqueName")]
static extern int PathMakeUniqueName (
ref string pszUniqueName,
int cchMax,
string pszTemplate,
string pszLongPlate,
string pszDir
);

[DllImport("shell32.dll", EntryPoint="PathYetAnotherMakeUniqueName")]
static extern int PathYetAnotherMakeUniqueName (
ref string pszUniqueName,
string pszPath,
string pszShort,
string pszFileSpec
);

public void Test()
{
DirectoryInfo working = new DirectoryInfo(@"..\..\");

string buffer = new string(' ', MAX_PATH);
string folderSpec = working.FullName;
string name = "New Text Document.txt";

PathYetAnotherMakeUniqueName(ref buffer, folderSpec, null, name);

}

~ Paul
Nov 16 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Hi Paul,
I'm having trouble getting the api PathYetAnotherMakeUniqueName to work.
Here is what I have so far. It is not working. What am i missing?
Use a StringBuilder for the first argument:
[DllImport("shell32.dll", EntryPoint="PathMakeUniqueName")]
static extern int PathMakeUniqueName (
StringBuilder pszUniqueName,
int cchMax,
string pszTemplate,
string pszLongPlate,
string pszDir
);

StringBuffer buffer = new StringBuffer(MAX_PATH);
...
PathYetAnotherMakeUniqueName(buffer, folderSpec, null, name);

bye
Rob
Nov 16 '05 #2

P: n/a
Hi,

Using StringBuilder allows the call to return correctly but the value is not
correct. It returns just the path and not the file name. Any ideas?

StringBuilder buffer = new StringBuilder(MAX_PATH);
PathYetAnotherMakeUniqueName(buffer, "D:\", null, "File.txt");

The value of buffer after call is just "D:\"

thanks,
~ Paul
"Robert Jordan" <ro*****@gmx.net> wrote in message
news:cl*************@news.t-online.com...
Hi Paul,
I'm having trouble getting the api PathYetAnotherMakeUniqueName to work.
Here is what I have so far. It is not working. What am i missing?


Use a StringBuilder for the first argument:
[DllImport("shell32.dll", EntryPoint="PathMakeUniqueName")]
static extern int PathMakeUniqueName (


StringBuilder pszUniqueName,
int cchMax,
string pszTemplate,
string pszLongPlate,
string pszDir
);

StringBuffer buffer = new StringBuffer(MAX_PATH);
...
PathYetAnotherMakeUniqueName(buffer, folderSpec, null, name);

bye
Rob

Nov 16 '05 #3

P: n/a
Hi Paul,
Using StringBuilder allows the call to return correctly but the value is not
correct. It returns just the path and not the file name. Any ideas?

StringBuilder buffer = new StringBuilder(MAX_PATH);
PathYetAnotherMakeUniqueName(buffer, "D:\", null, "File.txt");

The value of buffer after call is just "D:\"


Please post the unmanaged declaration.

bye
Rob
Nov 16 '05 #4

P: n/a
Here is the code i'm testing with ...

const int MAX_PATH = 260;

[DllImport("shell32.dll", EntryPoint="PathYetAnotherMakeUniqueName")]
static extern int PathYetAnotherMakeUniqueName (
StringBuilder pszUniqueName,
string pszPath,
string pszShort,
string pszFileSpec);

public void Test()
{
DirectoryInfo working = new DirectoryInfo(@"..\..\");

StringBuilder buffer = new StringBuilder(MAX_PATH);
string folderSpec = @"D:\New Text Document.txt";
string fileSpec = "New Text Document.txt";

int result = PathYetAnotherMakeUniqueName(buffer, folderSpec, null,
fileSpec);

Debug.WriteLine(buffer.ToString());
}

I've read through the documentation a little more and it said that the
filename had to be included in the path. I did so but it is still not
working. Now, it just outputs the filename again. It does not give me a
unique name.

thanks for your help,
Paul

"Robert Jordan" <ro*****@gmx.net> wrote in message
news:cl*************@news.t-online.com...
Hi Paul,
Using StringBuilder allows the call to return correctly but the value is
not correct. It returns just the path and not the file name. Any ideas?

StringBuilder buffer = new StringBuilder(MAX_PATH);
PathYetAnotherMakeUniqueName(buffer, "D:\", null, "File.txt");

The value of buffer after call is just "D:\"


Please post the unmanaged declaration.

bye
Rob

Nov 16 '05 #5

P: n/a
Hi Paul,
Here is the code i'm testing with ...


The unmanaged C declaration please :-))

bye
Rob
Nov 16 '05 #6

P: n/a
The unmaaged C declaration is ...

BOOL PathYetAnotherMakeUniqueName(
LPWSTR pszUniqueName,
LPCWSTR pszPath,
LPCWSTR pszShort,
LPCWSTR pszFileSpec
);

http://msdn.microsoft.com/library/de...uniquename.asp

thanks
Paul
"Robert Jordan" <ro*****@gmx.net> wrote in message
news:cl*************@news.t-online.com...
Hi Paul,
Here is the code i'm testing with ...


The unmanaged C declaration please :-))

bye
Rob

Nov 16 '05 #7

P: n/a
Hi Paul,

http://msdn.microsoft.com/library/de...uniquename.asp


It doesn't work for me either, but this is not a p/invoke or Interop
problem: it doesn't work from a C++ application too.

bye
Rob
Nov 16 '05 #8

P: n/a
Interesting. I wonder what trick it takes to get it to work. I've tried
PathMakeUniqueName too and it doesn't seem to work either.

thanks
Paul

"Robert Jordan" <ro*****@gmx.net> wrote in message
news:cl************@news.t-online.com...
Hi Paul,

http://msdn.microsoft.com/library/de...uniquename.asp


It doesn't work for me either, but this is not a p/invoke or Interop
problem: it doesn't work from a C++ application too.

bye
Rob

Nov 16 '05 #9

P: n/a
What do you mean it does not work from a c++ app? what error you get?

I made a simple c++ code, and it works for me.
///------------------------------------------------
#define UNICODE
#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <shlobj.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
DWORD lErr;
WCHAR oStr[MAX_PATH] = L"";
WCHAR p1[] = L"c:\\InoSetRTThread.log";
WCHAR pd[] = L"longfilename.log";

BOOL bRet = PathYetAnotherMakeUniqueName(oStr, p1,NULL, pd);

if( !bRet)
{
lErr = GetLastError();
char buf[32];
itoa(lErr,buf,16);
OutputDebugStringA(buf);
}
else
OutputDebugStringW(oStr);

return 0;
}
// eof

Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.

Nov 16 '05 #10

P: n/a
Hi Rhett,
What do you mean it does not work from a c++ app? what error you get?


No errors. I simply don't understand what the function is supposed
to do. I just wanted to tell the OP there is no Interop related problem.

bye
Rob
Nov 16 '05 #11

P: n/a
Hi,

I was able to get the c++ version to work. I followed that exact same
syntax converted to c# and the c# version doesn't work. It simply gives
back the file name, even if it exits in the target directory.

Using c++ code provided, the output is "c:\longfilename (2).log". The
output of the c# version is "longfilename.log". There must be some Interop
issue. Anyone have any other ideas as to what to try?

thanks
Paul

"Rhett Gong [MSFT]" <v-******@online.microsoft.com> wrote in message
news:6e**************@cpmsftngxa10.phx.gbl...
What do you mean it does not work from a c++ app? what error you get?

I made a simple c++ code, and it works for me.
///------------------------------------------------
#define UNICODE
#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <shlobj.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
DWORD lErr;
WCHAR oStr[MAX_PATH] = L"";
WCHAR p1[] = L"c:\\InoSetRTThread.log";
WCHAR pd[] = L"longfilename.log";

BOOL bRet = PathYetAnotherMakeUniqueName(oStr, p1,NULL, pd);

if( !bRet)
{
lErr = GetLastError();
char buf[32];
itoa(lErr,buf,16);
OutputDebugStringA(buf);
}
else
OutputDebugStringW(oStr);

return 0;
}
// eof

Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no
rights.
Please reply to newsgroups only. Thanks.

Nov 16 '05 #12

P: n/a
The function is used to create a unique filename based on an existing
filename in the same path.
If you put a string in pszFileSpec, the output will base on the
pszFileSpec. But if you pass NULL in pszFileSpec , a unique filename is
created based on the existing filename.
eg. I put "c:\\InoSetRTThread.log" in pszPath and pass "longfilename.log"
in pszFileSpec then the output should be "longfilename.log" if there is no
file exist in the path with the same filename. Otherwise "longfilename
(2).log" will be generated.
Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.

Nov 16 '05 #13

P: n/a
I was able to figure out the problem. It was an Interop issue. You simple
have to add CharSet=CharSet.Unicode to the DllImport attribute.

[DllImport("shell32.dll", EntryPoint="PathYetAnotherMakeUniqueName",
CharSet=CharSet.Unicode)]
internal static extern bool PathYetAnotherMakeUniqueName (
StringBuilder pszUniqueName,
string pszPath,
string pszShort,
string pszFileSpec );

After making this change, it works as expected.

thanks for the help,
Paul

"Paul Welter" <lo******@msdn.nospam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

I was able to get the c++ version to work. I followed that exact same
syntax converted to c# and the c# version doesn't work. It simply gives
back the file name, even if it exits in the target directory.

Using c++ code provided, the output is "c:\longfilename (2).log". The
output of the c# version is "longfilename.log". There must be some
Interop issue. Anyone have any other ideas as to what to try?

thanks
Paul

"Rhett Gong [MSFT]" <v-******@online.microsoft.com> wrote in message
news:6e**************@cpmsftngxa10.phx.gbl...
What do you mean it does not work from a c++ app? what error you get?

I made a simple c++ code, and it works for me.
///------------------------------------------------
#define UNICODE
#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <shlobj.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
DWORD lErr;
WCHAR oStr[MAX_PATH] = L"";
WCHAR p1[] = L"c:\\InoSetRTThread.log";
WCHAR pd[] = L"longfilename.log";

BOOL bRet = PathYetAnotherMakeUniqueName(oStr, p1,NULL, pd);

if( !bRet)
{
lErr = GetLastError();
char buf[32];
itoa(lErr,buf,16);
OutputDebugStringA(buf);
}
else
OutputDebugStringW(oStr);

return 0;
}
// eof

Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no
rights.
Please reply to newsgroups only. Thanks.


Nov 16 '05 #14

P: n/a
Hi Paul.

It was nice to hear that you have had the problem resolved. Thanks for
sharing your experience with all the people here. If you have any
questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.