小编Dan*_*arp的帖子

Kernel32 CopyFile找不到存在的文件c#

我调用了Kernel32的复制文件方法:

[DllImport("kernel32.dll",
           CharSet = CharSet.Unicode,
           CallingConvention = CallingConvention.StdCall,
           SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool CopyFile(
                           [MarshalAs(UnmanagedType.LPStr)] string lpExistingFileName,
                           [MarshalAs(UnmanagedType.LPStr)] string lpNewFileName,
                           [MarshalAs(UnmanagedType.Bool)] bool bFailIfExists);
[DllImport("kernel32.dll")]
    public static extern uint GetLastError();
Run Code Online (Sandbox Code Playgroud)

但是,当我调用它时,它从GetLastError()返回2,这意味着找不到文件.路径肯定存在.

string newfile = Environment.CurrentDirectory + "\\temp" + Path.GetExtension(file);
uint i;
if (!CopyFile(file, newfile, true)) i = GetLastError();
Run Code Online (Sandbox Code Playgroud)

我试图通过这个解决方案绕过LongPath异常.但即使使用普通文件,它似乎也不起作用.任何帮助,将不胜感激.

这是Form1的完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Shell32;
using System.Xml;
using System.Diagnostics;
using word = Microsoft.Office.Interop.Word; …
Run Code Online (Sandbox Code Playgroud)

c# windows pinvoke winapi

0
推荐指数
1
解决办法
1037
查看次数

标签 统计

c# ×1

pinvoke ×1

winapi ×1

windows ×1