Freakishly
Freakishly

Reputation: 1571

How can I add text to the file I create using IFileOperation?

I looked at the MSDN samples for IFileOperation, and they show how to create a file, but I don't want to create a blank file, I want to add some text in it. Here's what I have from the sample:

IFileOperation *pfo;
HRESULT hr = CreateAndInitializeFileOperation(IID_PPV_ARGS(&pfo));
if (SUCCEEDED(hr))
{
    hr = pfo->NewItem(psiFolder, FILE_ATTRIBUTE_NORMAL, c_szFileName, NULL, NULL);
    if (SUCCEEDED(hr))
    {
        hr = pfo->PerformOperations();
    }
    pfo->Release();
}

In the pfo->NewItem call, the fourth parameter is for the template, but I don't want to define a template.I want to create a .Url file with the content:

[InternetShortcut]
URL=www.google.com

I can't even seem to control the format of the file, let alone the content. Please help!

Upvotes: 0

Views: 355

Answers (1)

Jonathan Potter
Jonathan Potter

Reputation: 37192

IFileOperation::NewItem lets you access the functionality of the 'ShellNew' menu (the menu you get if you right-click in a folder, and select New from the context menu). It's designed to let you make a folder or an empty file, or create a new file from a pre-existing template. It doesn't let you make a file containing arbitrary data.

Once you've created a new empty file you could open it using CreateFile() and write your data to it using WriteFile(). But note that CreateFile() could create the file for you as well - there's really no need to use IFileOperation simply to create an empty file (unless you need its support for UAC to create the file in a protected location, but in that case you'll have trouble writing to it afterwards).

Upvotes: 2

Related Questions