在 Windows 7 中的 c:\program files\myApp\data 中创建文件夹和文件

发布于 2024-10-10 13:49:32 字数 913 浏览 11 评论 0 原文

我有一个旧的 C++ 应用程序,需要修改才能与 Windows 7 一起使用。 问题在于创建一个新文件夹并将文件保存在该文件夹中。该文件夹应创建于

c:\program files\myApp\data\newFolder。

这是我用来创建新文件夹并出现错误的函数:

if(!CreateDirectory(pathSamples,NULL))  //Throw Error
 {
  DWORD errorcode = GetLastError(); 
  LPVOID lpMsgBuf;

  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
       NULL, errorcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL );

  MessageBox(NULL, (LPCTSTR)lpMsgBuf, TEXT("Error"), MB_OK);
 }

在 XP 中可以使用,但在 Windows 7 中则不行。如果我以管理员身份运行应用程序,则会创建该文件夹,否则会引发“访问被拒绝”错误。

我的问题如下:
是否有一个选项可以更改代码,以便可以在“程序文件”中创建该文件夹,并且可以将文件保存在此文件夹中?

PS我看到此线程已经,但它没有回答我的问题。

谢谢,
伊利亚

I have an old c++ application that needs to be modified to work with windows 7.
Problem is in creating a new folder and saving a file in that folder. This folder should be created in

c:\program files\myApp\data\newFolder.

This is function I use to create new folder and get errors:

if(!CreateDirectory(pathSamples,NULL))  //Throw Error
 {
  DWORD errorcode = GetLastError(); 
  LPVOID lpMsgBuf;

  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
       NULL, errorcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL );

  MessageBox(NULL, (LPCTSTR)lpMsgBuf, TEXT("Error"), MB_OK);
 }

In XP this works, but in Windows 7 it doesn't. If I run application as administrator than the folder is created, otherwise "Access is denied" error is thrown.

My question is following:
Is there an option to make changes to the code so that the folder can be created in "program files" nad that files can be saved in this folder?

PS I saw this thread already but it doesn't answer my question.

Thanks,
Ilija

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

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

发布评论

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

评论(5

傲世九天 2024-10-17 13:49:33

你已经回答了你自己的问题。您需要成为管理员才能在 Windows 7 中的 Program Files 下进行写入。

应用程序数据位于 Users//AppData 等下的不同区域中...

您始终可以使用注册表来选择写入位置,因此您可以使用旧的XP 上的区域以及 Vista 和 Windows 7 上的新区域。

You have answered your own question. You need to be an administrator to write under Program Files in Windows 7.

Application data goes in a different area under Users//AppData etc...

You can always use the registry to select the location to write, so you can use the old area on XP and the new area on Vista and Windows 7.

鸢与 2024-10-17 13:49:33

由于 Vista 及更高版本下的用户访问权限有限,您不希望尝试将文件放入“程序文件”或任何其他非标准位置。您确实应该使用 SHGetFolderPath 从系统获取正确的位置。

With limited user access under Vista and later you don't want to be trying to put files in "Program Files" or any other non-standard place. You should really be using SHGetFolderPath to obtain the correct location from the system.

飞烟轻若梦 2024-10-17 13:49:33

正如其他人已经写的那样,%ProgramFiles% 不是存储用户数据的正确位置。正确的解决方案显然是重新设计应用程序,以便它使用不同的存储位置。

作为替代方案,存在一个快速而肮脏的(!)修复:如果应用程序没有清单,则用户帐户控制数据重定向将启动,透明地将对系统区域的写入请求重定向到安全位置用户个人资料。重定向目标为 %LocalAppData%\VirtualStore\Program Files。有关此类内置虚拟化的详细信息,请参见 此处

因此,您只需从应用程序中删除清单即可完成。

As others already wrote, %ProgramFiles% is not the right place to store user data. The correct solution obviously is to redesign the application so that it uses a different storage location.

As an alternative there exists a quick and dirty (!) fix: If the application does not have a manifest, User Account Control Data Redirection kicks in, transparently redirecting write requests to system areas to a safe place in the user profile. The redirection target is %LocalAppData%\VirtualStore\Program Files. Details about this kind of built-in virtualization can be found here.

So you could be done by simply removing the manifest from your application.

橘味果▽酱 2024-10-17 13:49:33

作为 @CashCow 写道:

您需要成为管理员才能
在 Windows 中的 Program Files 下写入
7.

最好的方法是提升进程(使用 ShellExecute "runas" 或类似命令),然后创建文件夹。

一些 ShellExecute 示例:

As @CashCow writes:

You need to be an administrator to
write under Program Files in Windows
7.

Best way to do this is to elevate your process (using ShellExecute "runas" or similar), and then create the folder.

Some ShellExecute examples:

可爱暴击 2024-10-17 13:49:33

看起来在安装程序中设置此文件夹的权限就足够了,现在它可以正常工作了。

谢谢大家的回答!

It looks like it was enough to set permissions for this folder in installer and now it works normally.

Thanks everyone for your answers!

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