防止程序在启动时被 Windows 克隆

时间:2021-04-13 12:07:32

标签: c# windows windows-10 registry startup

我在原程序的基础上设计了一个小程序,你可以在下面看到

private void RegisterAtStartupButton_Click(object sender, EventArgs e)
{
    var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    var value = key.GetValue(Application.ProductName, false)?.ToString();

    if (value == null)
    {
        key.SetValue(Application.ProductName, Application.ExecutablePath);
    }

    var path = Path.Combine(Directory.GetCurrentDirectory(), "Memory.txt");

    var text = File.Exists(path)
        ? File.ReadAllText(path)
        : "1";

    MessageBox.Show(text);

    File.WriteAllText(path, (Convert.ToInt32(text) + 1).ToString());
}

这个程序统计这个方法的执行次数并保存在一个文本文件中

此外,在 Windows 启动中注册该程序以在重新启动 Windows 并登录到相同的用户帐户后运行它。

通过重新启动Windows并登录同一帐户,程序将自动运行,现在如果我们单击调用该函数的按钮,则会显示以下错误

System.UnauthorizedAccessException: Access to the path 'C:\WINDOWS\system32\Memory.txt' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
   at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost)
   at System.IO.File.WriteAllText(String path, String contents)
   at StartupCounter.MainForm.MainForm_Load(Object sender, EventArgs e) in C:\Users\User\Source\Repos\StartupCounter\StartupCounter\MainForm.cs:line 23
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

当程序在另一个驱动器上时,Windows 是否将程序克隆到“C:\WINDOWS\system32\Memory.txt”?

我怎样才能防止这种情况发生并解决问题?

1 个答案:

答案 0 :(得分:1)

我敢打赌,当它工作时,您第一次运行程序是从 Memory.txt 所在的文件夹中运行的。这样,当您执行 GetCurrentDirectory 时,它会找到该文件。

但是,在启动情况下重启并运行时,当前目录是systems目录。

异常没有帮助,但我想您无权在这种情况下从那里读取。

因此要解决此问题,您无需使用 GetCurrentDirectory,而是使用完整路径。

或者更好的是,将 Memory.txt 存储在应用程序数据的特定位置,参见 Where is the correct place to store my application specific data?