.NET
Microsoft Technologies based on the .NET software framework.
3,449 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
A community member has associated this post with a similar question:
How to copy big file 5g with task and within impersonation
Only moderators can edit this content.
Hi,
I'm using net 8 C#.
I copy file in two steps:
From source to temp folder in C drive.
From temp folder to detination(USB) using impersonation.
When I copy small file is ok but when I copy big file 5G
I got exception:System.IO.IOException: 'The parameter is incorrect.
This the second step what wrong with it.
// Second step: temp to destination
if (SelectedDestinationDriveItem.DriveInfoValue.DriveType == DriveType.Removable)
{
// impersonation
#if WINDOWS
WindowsIdentity.RunImpersonated(
ImpersonationHelper.Token,
() =>
{
var x2 = WindowsIdentity.GetCurrent().Name;
// Copy files from temp folder to destination
filesList = concatFiles.Split('?', StringSplitOptions.RemoveEmptyEntries);
foreach (var file in filesList)
{
var destinationPathWithFileExtension = Path.Combine(SelectedDestinationItem.Path == null ?
SelectedDestinationItem.Name.TrimEnd('\\').TrimEnd('\\') : SelectedDestinationItem.Path, file.Replace(tempFolderFullPath, string.Empty).TrimStart('\\').TrimStart('\\'));
var dirPathWithoutFileExtension = Path.GetDirectoryName(destinationPathWithFileExtension);
if (!Directory.Exists(dirPathWithoutFileExtension))
{
Directory.CreateDirectory(dirPathWithoutFileExtension);
}
if (!File.Exists(destinationPathWithFileExtension))
{
File.Copy(file, destinationPathWithFileExtension, true);
}
else
{
int count = 1;
while (File.Exists(destinationPathWithFileExtension))
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(destinationPathWithFileExtension);
string fileExtension = Path.GetExtension(destinationPathWithFileExtension);
destinationPathWithFileExtension = Path.Combine(dirPathWithoutFileExtension, $"{fileNameWithoutExtension}({count}){fileExtension}");
count++;
}
File.Copy(file, destinationPathWithFileExtension, true);
}
}
});
#endif