在 Windows 7 上访问 C:\ 驱动器

发布于 01-06 14:52 字数 819 浏览 7 评论 0 原文

我在 Windows 7 计算机上拥有完全管理员权限,但当我运行在 c:\ 驱动器上创建文件的应用程序时,我收到错误代码 5(访问被拒绝)。我知道 Windows 7 不允许在 C 盘和程序文件等受保护区域中创建文件,如果我从其他地方复制文件,文件资源管理器会显示“管理”消息框,之后它允许,但我的应用程序可以获得写入级别使用权?

在我的应用程序中,用户可以选择他们想要创建文件的文件夹,因此如果他们选择 c:\ 驱动器,他/她显然会收到此错误,这是不希望的。

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);
    }

}

据我研究,我似乎无法绕过 UAC 对话框,这很好,但我的应用程序甚至不显示它(这也是可以理解的),但是我的选择是什么?

我发现我唯一的选择是在我自己的应用程序中检测它(如果这是 Windows 7 操作系统),然后在创建文件之前检查文件路径,并显示一条更用户友好的消息“Windows 7 不喜欢您在此文件夹中创建文件” ,选择不同的文件夹或返回到 xp'。这个方案是 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

捎一片雪花 2025-01-13 14:52:29

正如 Kolink 指出的,您的应用程序需要以管理员权限运行。为了自动执行此操作,请按照此处的说明嵌入清单。

编辑:对于 VS2010:项目属性>配置属性>链接器>清单文件将“UAC 执行级别”更改为所需的值。

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.

恍梦境° 2025-01-13 14:52:29

要么不要尝试写入受保护的区域,要么要求您的应用程序以权限运行(右键单击 => 以管理员身份运行)。

我知道我不喜欢随机文件出现在我的根目录中 - 我喜欢组织好的文件。

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.

此岸叶落 2025-01-13 14:52:29

如果是用户提供路径,那么您应该通知他们该文件无法保存到此位置,并要求提供另一个名称。

通常是 shell,GetSaveFileName 函数,返回前检查是否可以在所选目录中创建新文件,参见="">OFN_NOTESTFILECREATE href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646839.aspx" rel="nofollow">OPENFILENAME 结构。

另一种选择是处理这种情况并自行显示 UAC 确认。但这个解决方案需要付出比其实际价值更多的努力。您无法提升当前进程,因此将文件保存到受保护区域的操作必须在另一个进程中实现。同时你当前的进程有数据需要保存,所以你必须实现两个进程之间的通信。阅读为 Windows Vista 设计 UAC 应用程序更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文