Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

c# - CopyFileEx "The parameter is invalid" error

I'm writing a (fairly) simple C# application using .NET 4 to check for updates before running an executable. If a newer version of an exe exists on a network share, simply copy it over to the local folder and start it. It's all working perfectly, except while reading about the limitations of File.Copy() I realized that I wasn't going to be able to show a progress bar while I did this, and everything I saw said to use CopyFileEx, so I'm trying to do that.

I used the sample code found here and it compiles fine (although I'm still a little unsure of exactly how the backgroundworker comes into play), except when I actually go to run the application, the CopyFilEx() method returns false, with the error being "The parameter is incorrect".

My code (relevant sections only, I'll add more if need be)

Calling the function:

XCopy.Copy(strServerAppPath + strExeName, strLocalAppPath + strExeName, true, true, (o,    pce) =>
{
worker.ReportProgress(pce.ProgressPercentage, strServerAppPath + strExeName);
});

(the source path evaluates to "C:est.txt" and the destination path to "C:estest.txt")

Where the error occurs in the code linked above:

bool result = CopyFileEx(Source, Destination, new CopyProgressRoutine(CopyProgressHandler), IntPtr.Zero, ref IsCancelled, copyFileFlags);
            if (!result)
                throw new Win32Exception(Marshal.GetLastWin32Error());

Thanks in advance for the help, I've been struggling with this for a few hours now...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Rather than deal with all that Marshalling, it's pretty trivial to just "roll your own" copier that goes chunk by chunk:

private static void CopyFile(string source, string destination, int bytesPerChunk)
{
    int bytesRead = 0;

    using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read))
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            using (FileStream fsDest = new FileStream(destination, FileMode.Create))
            {
                BinaryWriter bw = new BinaryWriter(fsDest);
                byte[] buffer;

                for (int i = 0; i < fs.Length; i += bytesPerChunk)
                {
                    buffer = br.ReadBytes(bytesPerChunk);
                    bw.Write(buffer);
                    bytesRead += bytesPerChunk;
                    ReportProgress(bytesRead, fs.Length);  //report the progress
                }
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...