未能加载文件或程序集“file:///C:\Windows\System32\kernel32.dll”或它的某一个依赖项。该模块应包含一个程序集清单

使用问题 · 600 次浏览
白起1996 创建于 2023-04-16 15:24

不知道以下代码哪里出错了。在visual studio是可以运行的

using System;
using System.IO;
using System.Runtime.InteropServices;

[DllImport("kernel32.dll")]
static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags);

[DllImport("kernel32.dll")]
static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);
        
public static void Exec(Quicker.Public.IStepContext context)
{
	// 获取输入参数
    string filePath = @"E:\杂项\工作簿1.xlsx"; // 需要创建链接的文件或文件夹路径
    string linkType = "hard"; // 链接类型,"soft" 为软链接,"hard" 为硬链接
    string targetFolder = @"E:\杂项"; // 目标文件夹路径

    // 检查输入参数
    if (string.IsNullOrEmpty(filePath) || string.IsNullOrEmpty(linkType) || string.IsNullOrEmpty(targetFolder))
    {
        string error = "参数错误:请输入需要创建链接的文件或文件夹路径、链接类型('soft' 或 'hard')以及目标文件夹路径";
        Console.WriteLine(error);
        return;
    }

    if (!File.Exists(filePath) && !Directory.Exists(filePath))
    {
        string error = "文件或文件夹不存在,请检查输入路径是否正确";
        Console.WriteLine(error);
        return;
    }

    if (linkType != "soft" && linkType != "hard")
    {
        string error = "链接类型错误:请输入 'soft' 或 'hard',soft 为软链接,hard 为硬链接";
        Console.WriteLine(error);
        return;
    }

    if (!Directory.Exists(targetFolder))
    {
        string error = "目标文件夹不存在,请检查目标文件夹路径是否正确";
        Console.WriteLine(error);
        return;
    }

    string targetFilePath = Path.Combine(targetFolder, Path.GetFileName(filePath));

    // 若目标文件夹存在同名文件或同名文件夹,则在名字中加入序号
    int count = 1;
    while (File.Exists(targetFilePath) || Directory.Exists(targetFilePath))
    {
        string newFileName = $"{Path.GetFileNameWithoutExtension(filePath)}-{count}{Path.GetExtension(filePath)}";
        targetFilePath = Path.Combine(targetFolder, newFileName);
        count++;
    }

    // 创建链接
    try
    {
        if (linkType == "soft")
        {
            CreateSymbolicLink(targetFilePath, filePath, 0);
        }
        else
        {
            if (File.GetAttributes(filePath).HasFlag(FileAttributes.Directory))
            {
                string error = "不能为文件夹创建硬链接";
                throw new ArgumentException(error);
            }

            string fileDrive = Path.GetPathRoot(filePath);
            string targetDrive = Path.GetPathRoot(targetFilePath);

            if (fileDrive != targetDrive)
            {
                string error = "不能跨盘创建硬链接";
                throw new ArgumentException(error);
            }

            CreateHardLink(targetFilePath, filePath, IntPtr.Zero);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return;
    }
}
白起1996 最后更新于 2023/4/16

回复内容
CL 2023-04-16 18:05
#1

调试运行一下看看什么报错?

我用这段代码测试,是有一点语法问题,普通版的c#不支持$""这样的字符串插值。 要用V2版的才可。


白起1996 回复 CL 2023-04-16 18:40 :


白起1996 2023-04-16 18:38
#2


CL 最后更新于 2023-04-16 18:45
CL 回复 白起1996 2023-04-16 18:46 :

白起1996 回复 CL 2023-04-16 18:48 :

c:\Windows\Temp\CSSCRIPT\dynamic\2460.a92530ef-d9e4-48c5-93dc-d9e4609b6753.tmp(54,30): error CS1056: 意外的字符“$”

c:\Windows\Temp\CSSCRIPT\dynamic\2460.a92530ef-d9e4-48c5-93dc-d9e4609b6753.tmp(10,1): error CS1031: 应输入类型

c:\Windows\Temp\CSSCRIPT\dynamic\2460.a92530ef-d9e4-48c5-93dc-d9e4609b6753.tmp(10,1): error CS1519: 类、结构或接口成员声明中的标记“[”无效

删掉之后,报三个错误。

第一个是不是删掉$就好,其它两个怎么解决?

CL 回复 白起1996 2023-04-16 18:49 :


白起1996 回复 CL 2023-04-16 18:52 :

可以了,多谢!!!!!!!!!

回复主贴