zar
zar

Reputation: 12227

Accessing C:\ drive on Windows 7

I have full admin privileges on my Windows 7 machine but when I run my application which creates a file on c:\ drive I get error code 5 (Access is denied). I know windows 7 doesn't allow creating files in protected areas like c drive and program files and file explorer brings up 'administrative' message box if I copy a file there from somewhere else after which it does allows but can my application obtain write level access?

In my application, user gets to pick the folder where they want to create the file so if they choose c:\ drive s/he will obviously get this error which is not desirable.

void CTestDlg::OnBnClickedButtonCreate()
{
    CFile f;
    CFileException e;
    TCHAR* pszFileName = _T("c:\\test.txt"); // here i am hard coding path for simplicity.
    if(!f.Open(pszFileName, CFile::modeCreate | CFile::modeWrite, &e))
    {
        TRACE(_T("File could not be opened %d\n"), e.m_cause);
    }

}

As far as I have researched it seems I can't by-pass the UAC dialog which is fine but my application don't even present it (which is understandable as well) but what are my options?

I see my only option is to detect this in my own application if this is Windows 7 OS and than check for file path before creating the file and present a more user friendly message 'windows 7 doesn't like you to create file in this folder, choose a different folder or go back to xp'. Is this scheme the way to go on Windows 7? Is there any other way?

Upvotes: 1

Views: 9410

Answers (3)

Carey Gregory
Carey Gregory

Reputation: 6846

As Kolink noted, your application needs to run with administrator privileges. In order to do that automatically, embed a manifest as explained here.

EDIT: For VS2010: Project Properties > Configuration Properties > Linker > Manifest File Change the 'UAC Execution Level' to the desired value.

Upvotes: 3

Alexey Ivanov
Alexey Ivanov

Reputation: 11838

If it's the user who provides the path, then you should inform them that the file cannot be saved to this location and ask to provide another name.

Usually the shell, GetSaveFileName function, checks whether the new file can be created in the selected directory before returning, see flag OFN_NOTESTFILECREATE in description of OPENFILENAME structure.

Another option is to handle such situation and to show UAC confirmation yourself. But this solution requires much more effort than it's really worth. You can't elevate the current process, so the operation of saving the file to a protected area has to be implemented in another process. At the same time your current process has the data to be saved, so you'll have to implement the communication between the two processes. Read Designing UAC Applications for Windows Vista for more information.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

Either don't try to write to protected areas, or require that your application be run with permissions (right-click => Run as Administrator).

I know I don't like random files appearing in my root - I like my files organised.

Upvotes: 1

Related Questions