Primm
Primm

Reputation: 1387

How to cross platform copy a file in C#?

Basically I need to copy a file (FAT or NTFS either works) without using the Microsoft System.IO.File.Copy Libraries. I am working with COSMOS (C# open source managed operating system) and since that is not Windows, the File.Copy does not work.

Any help would be appreciated.

Upvotes: 0

Views: 466

Answers (1)

Isak Savo
Isak Savo

Reputation: 35884

Do you have any other part of the System.IO namespace? Most notably, the various Streams?

If not, then I fail to see how you should be able to copy anything.

But, assuming you can open files for reading and writing, you can implement your own copy method trivially:

private void CopyFile(string source, string dest)
{
    using (var input = new FileStream(source, FileMode.Open, FileAccess.Read))
    using (var output = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.Write))
    {
        byte[] data = new byte[1024];
        int bytesRead = 0;
        do
        {
            bytesRead = input.Read(data, 0, data.Length);
            if (bytesRead > 0)
                output.Write(data, 0, data.Length);
        } while (bytesRead > 0);
    }
}

(Above code not tested)

Upvotes: 2

Related Questions