You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#调用VSSadmin工具时触发“系统找不到指定文件”错误的技术咨询

解决VSSadmin调用时"The system cannot find the specified file"错误

咱们先拆解你遇到的问题:在WinForms的Form1_Load方法里调用vssadmin工具却提示找不到文件,这个问题主要和文件系统重定向以及权限限制两个核心点有关,下面一步步给你讲清楚怎么解决。

问题根源分析

  1. 32位程序的文件系统重定向陷阱
    如果你编译的WinForms程序是32位(默认AnyCPU在64位系统上运行时会自动以32位进程启动),Windows会把32位程序对System32文件夹的访问自动重定向到SysWOW64文件夹。但vssadmin.exe是64位专属工具,只存在于C:\Windows\System32目录下,32位程序直接写"vssadmin"会去SysWOW64找,自然找不到文件。

  2. 管理员权限缺失(后续必踩坑点)
    就算解决了路径问题,配置卷影副本存储需要管理员权限,否则执行vssadmin会直接报错,这个也是必须提前处理的环节。

具体解决办法

方法1:直接指定vssadmin的完整系统路径

通过系统API获取System32的真实路径,避免重定向问题:

private void Form1_Load(object sender, EventArgs e)
{
    // 获取System32文件夹的完整路径
    string vssadminPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "vssadmin.exe");
    
    using (Process p = new Process())
    {
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.FileName = vssadminPath;
        p.StartInfo.Arguments = "shadowstorage /for=C: /on=C: /maxsize=18%";
        
        try
        {
            p.Start();
            string errorOutput = p.StandardError.ReadToEnd();
            p.WaitForExit();
            
            if (p.ExitCode != 0)
            {
                MessageBox.Show($"执行错误: {errorOutput}");
            }
            else
            {
                MessageBox.Show("卷影副本存储配置成功!");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show($"启动进程失败: {ex.Message}");
        }
    }
}

方法2:临时禁用文件系统重定向(适合频繁调用系统工具的场景)

通过Windows API暂时关闭32位程序的文件系统重定向,让程序直接访问System32目录:

// 先添加Kernel32 API的引用
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);

private void Form1_Load(object sender, EventArgs e)
{
    IntPtr wow64Ptr = IntPtr.Zero;
    try
    {
        // 禁用32位程序的文件系统重定向
        Wow64DisableWow64FsRedirection(ref wow64Ptr);
        
        using (Process p = new Process())
        {
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = "vssadmin";
            p.StartInfo.Arguments = "shadowstorage /for=C: /on=C: /maxsize=18%";
            
            p.Start();
            string errorOutput = p.StandardError.ReadToEnd();
            p.WaitForExit();
            
            if (p.ExitCode != 0)
            {
                MessageBox.Show($"执行错误: {errorOutput}");
            }
            else
            {
                MessageBox.Show("卷影副本存储配置成功!");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show($"错误: {ex.Message}");
    }
    finally
    {
        // 必须恢复重定向,避免影响其他系统调用
        if (wow64Ptr != IntPtr.Zero)
        {
            Wow64RevertWow64FsRedirection(wow64Ptr);
        }
    }
}

必做额外步骤:添加管理员权限要求

右键你的WinForms项目 → 添加新项 → 选择「应用程序清单文件」,修改<requestedExecutionLevel>节点:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

这样程序启动时会自动弹出UAC请求管理员权限,避免后续执行vssadmin时因权限不足失败。

调试小技巧

  • 先手动以管理员身份打开命令提示符,执行vssadmin shadowstorage /for=C: /on=C: /maxsize=18%,确认命令本身是否能正常执行,排除参数错误的可能。
  • 检查程序编译平台:右键项目 → 属性 → 生成 → 平台目标,改成x64或者取消AnyCPU的「首选32位」勾选,也能绕开文件系统重定向问题。

内容的提问来源于stack exchange,提问作者Edson Ordaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 20:44:05