修复了只读文件夹

时间:2015-09-04 06:10:25

标签: c# visual-studio-2015

我正在尝试复制或移动我的C#应用​​程序中的文件夹和文件,但文件夹是只读,并且无法禁用它们,因为当我尝试时,它会再次启用。我尝试了很多解决方案,但没有工作......是的,我是管理员,拥有所有权利。我也试过在脚本中禁用只读,但是不起作用。

const int CopyBufferSize = 64 * 1024;
    public void CopyFile(string source, string destination)
    {
        //File.Copy(source, destination);
        //Stopwatch swTotal = Stopwatch.StartNew();
        using (var outputFile = File.Create(destination))
        {
            using (var inputFile = File.OpenRead(source))
            {
                // we need two buffers so we can ping-pong
                var buffer1 = new byte[CopyBufferSize];
                var buffer2 = new byte[CopyBufferSize];
                var inputBuffer = buffer1;
                int bytesRead;
                IAsyncResult writeResult = null;
                while ((bytesRead = inputFile.Read(inputBuffer, 0, CopyBufferSize)) != 0)
                {
                    // Wait for pending write
                    if (writeResult != null)
                    {
                        writeResult.AsyncWaitHandle.WaitOne();
                        outputFile.EndWrite(writeResult);
                        writeResult = null;
                    }
                    // Assign the output buffer
                    var outputBuffer = inputBuffer;
                    // and swap input buffers
                    inputBuffer = (inputBuffer == buffer1) ? buffer2 : buffer1;
                    // begin asynchronous write
                    writeResult = outputFile.BeginWrite(outputBuffer, 0, bytesRead, null, null);
                }
                if (writeResult != null)
                {
                    writeResult.AsyncWaitHandle.WaitOne();
                    outputFile.EndWrite(writeResult);
                }
            }
        }
        //swTotal.Stop();
        //Console.WriteLine("Total time: {0:N4} seconds.", swTotal.Elapsed.TotalSeconds);
    }

我试过

File.Copy(source, destination);

谢谢。

1 个答案:

答案 0 :(得分:1)

more details..

var fileName = "sourceFile.txt";
var source = Path.Combine(Environment.CurrentDirectory, fileName);
var destination = Path.Combine(destinationFolder, fileName);

File.Copy(source, destination);

OR

File.Copy(@"someDirectory\someFile.txt", @"otherDirectory\someFile.txt");