QFile::remove() 有效和无效
QFile::remove() works and doesn't work
我正在删除 windows 启动菜单中的快捷方式,作为 delete/create 的一部分,基于应用程序中的自动启动设置。
我最初是通过安装创建快捷方式的(我使用的是 InnoSetup)。问题是我的代码不会删除快捷方式。但是如果我自己删除快捷方式并让我的代码创建它而不是删除它就好了。但是快捷方式名称完全相同,我什至在代码中检查它是否存在并且每次都存在。我需要做什么才能在作为安装的一部分创建时删除它?
void SettingsDialog::on_checkBoxAutoStart_clicked()
{
QSettings settings;
QString startupFolder = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation) + "/Startup";
QString installPath = settings.value( INSTALL_PATH, "").toString();
// if path is empty, return
if (installPath.isEmpty() )
return;
QString appPath = installPath+ "\MyApp.exe";
// if the exe doesn't exist for any reason, return
if( !QFile(appPath).exists() )
return;
QString shortcutName = startupFolder + "/MyApp.lnk";
if ( ui->checkBoxAutoStart->checkState() == Qt::Checked )
{
QFile::link( appPath, shortcutName);
settings.setValue( AUTO_START, "true" );
}
else
{
QFile shortcut( shortcutName);
if ( !shortcut.exists() )
qDebug() << "shortcut don't exist";
int shortcut_permissions = shortcut.permissions();
shortcut.setPermissions(QFile::ReadUser | QFile::WriteUser | QFile::ExeUser | QFile::ExeUser);
shortcut_permissions = shortcut.permissions();
shortcut.remove();
qDebug() << shortcut.errorString();
settings.setValue( AUTO_START, "false" );
}
}
我是 运行 这个 Windows 7.
更新
创建快捷方式的 inno-setup 行是这样的(我已将我的应用程序名称重命名为 generic)。
[Icons]
Name: "{commonstartup}\MyApp"; Filename: "{app}\MyApp.exe"
TL;DR 问题完全在于您的安装程序和您错误地认为非特权进程(如您的 Qt 应用程序)可能会混淆常见的配置文件快捷方式等。
The installer does install it in current user profile.
这是错误的。请参阅 here 以供参考。
在 {commonstartup}
位置创建的快捷方式适用于所有用户,并且只能由进程 运行 以管理员身份修改。
如果您希望为当前用户创建快捷方式,请在{userstartup}
中创建它,然后您的进程,运行用户权限,将能够修改它。
请注意,仅仅因为您 运行 在管理帐户上,您的 Qt 进程 不是 运行 管理权限,除非您强行启动它,所以,或者除非您已经在其上设置了二进制标志以使 Windows 将其作为管理进程启动。
I would there has to be a way to remove the shortcut by the application so it auto starts or not, that's pretty standard.
它在 Windows 95 上非常标准。在 Windows XP 上,它不应该是,但没有人注意文档。自从 Vista 和 UAC 的到来,如果启动快捷方式在 Public
帐户(以前的 AllUsers
)中,非管理进程根本无法完成。
此外,您可以轻松地从资源管理器中删除或修改该快捷方式,因为您是管理员,但那是因为资源管理器 运行 具有管理功能。你的 Qt 进程没有。非管理(默认)命令提示符也不会。
我正在删除 windows 启动菜单中的快捷方式,作为 delete/create 的一部分,基于应用程序中的自动启动设置。
我最初是通过安装创建快捷方式的(我使用的是 InnoSetup)。问题是我的代码不会删除快捷方式。但是如果我自己删除快捷方式并让我的代码创建它而不是删除它就好了。但是快捷方式名称完全相同,我什至在代码中检查它是否存在并且每次都存在。我需要做什么才能在作为安装的一部分创建时删除它?
void SettingsDialog::on_checkBoxAutoStart_clicked()
{
QSettings settings;
QString startupFolder = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation) + "/Startup";
QString installPath = settings.value( INSTALL_PATH, "").toString();
// if path is empty, return
if (installPath.isEmpty() )
return;
QString appPath = installPath+ "\MyApp.exe";
// if the exe doesn't exist for any reason, return
if( !QFile(appPath).exists() )
return;
QString shortcutName = startupFolder + "/MyApp.lnk";
if ( ui->checkBoxAutoStart->checkState() == Qt::Checked )
{
QFile::link( appPath, shortcutName);
settings.setValue( AUTO_START, "true" );
}
else
{
QFile shortcut( shortcutName);
if ( !shortcut.exists() )
qDebug() << "shortcut don't exist";
int shortcut_permissions = shortcut.permissions();
shortcut.setPermissions(QFile::ReadUser | QFile::WriteUser | QFile::ExeUser | QFile::ExeUser);
shortcut_permissions = shortcut.permissions();
shortcut.remove();
qDebug() << shortcut.errorString();
settings.setValue( AUTO_START, "false" );
}
}
我是 运行 这个 Windows 7.
更新
创建快捷方式的 inno-setup 行是这样的(我已将我的应用程序名称重命名为 generic)。
[Icons]
Name: "{commonstartup}\MyApp"; Filename: "{app}\MyApp.exe"
TL;DR 问题完全在于您的安装程序和您错误地认为非特权进程(如您的 Qt 应用程序)可能会混淆常见的配置文件快捷方式等。
The installer does install it in current user profile.
这是错误的。请参阅 here 以供参考。
在 {commonstartup}
位置创建的快捷方式适用于所有用户,并且只能由进程 运行 以管理员身份修改。
如果您希望为当前用户创建快捷方式,请在{userstartup}
中创建它,然后您的进程,运行用户权限,将能够修改它。
请注意,仅仅因为您 运行 在管理帐户上,您的 Qt 进程 不是 运行 管理权限,除非您强行启动它,所以,或者除非您已经在其上设置了二进制标志以使 Windows 将其作为管理进程启动。
I would there has to be a way to remove the shortcut by the application so it auto starts or not, that's pretty standard.
它在 Windows 95 上非常标准。在 Windows XP 上,它不应该是,但没有人注意文档。自从 Vista 和 UAC 的到来,如果启动快捷方式在 Public
帐户(以前的 AllUsers
)中,非管理进程根本无法完成。
此外,您可以轻松地从资源管理器中删除或修改该快捷方式,因为您是管理员,但那是因为资源管理器 运行 具有管理功能。你的 Qt 进程没有。非管理(默认)命令提示符也不会。