Solved

How do I use CFIle with path lengths greater than MAX_PATH?

Posted on 2004-04-01
4
895 Views
Last Modified: 2013-11-20
I need to be able to save files into an absolute location whose full path is more than MAX_PATH characters.  My application is not (and can not easily be) in Unicode.  Could I use a relative path name of fewer than MAX_PATH characters and if so, what is the best way to set the desired current directory.  My appication is currently using CFile for file handling.  Please provide a few lines of code by way of an example if not very straightforward.
0
Comment
Question by:PhilipG
4 Comments
 
LVL 15

Assisted Solution

by:lakshman_ce
lakshman_ce earned 100 total points
ID: 10734754
Plz have a look at this for obtaining relative file names,
http://www.codeguru.com/Cpp/misc/misc/fileanddirectorynaming/print.php/c263/

-Lakshman
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10735729
You could try _chdir to set the current directory in stages
eg.
_chdir("C:\\temp");
_chdir(".\\subDir1");
_chdir(".\\SubDir2");
and so on where you split your path (recursive function) and move down the directory chain for each directory.
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 150 total points
ID: 10736448
You'll still run into problems because CFile::Open() calls a function named AfxFullPath() that expands the file name to the full path with the limitation of _MAX_PATH. Look at FILECORE.CPP in the MFC\SRC directory.

What you might try is to derive a new class from CFile and override the Open method fixing the _MAX_PATH limitations. You'll want to look at the FILECORE.CPP to see what else you'd have to override/fix.

Then you could derive from your new class and attempt to open the long file name.

Good Luck,
Steve
0
 

Author Comment

by:PhilipG
ID: 10737161
Thanks all for the very quick response.  I have awarded the points although in the end I adopted a different solution.  The reason for the high point was that I wanted ideas very quickly - it worked.

The solution I used was to use CreateFileW and to convert my path to Unicode using the A2W macro.  I was also able to set the attributes by using SetFileAttributesW and the Unicode path.  The Microsoft info is:

HANDLE CreateFile(
  LPCTSTR lpFileName,
  DWORD dwDesiredAccess,
  DWORD dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD dwCreationDisposition,
  DWORD dwFlagsAndAttributes,
  HANDLE hTemplateFile
);

Parameters
lpFileName
[in] Pointer to a null-terminated string that specifies the name of the object to create or open.
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

Thanks for the quick response.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CRON statement to run every 3 minutes except on Monday morning 12am to 2am 7 114
has22 challenge 11 107
lucky13 challenge 11 158
tripleUp challenge 7 93
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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