为什么下面的代码导致:
public static class Program
{
public static void Main(params string[] args)
{
var sourceFileName = @"C:\Users\ehoua\Desktop\Stuff\800MFile.exe";
var destinationFileName = sourceFileName + ".bak";
FileCopyAsync(sourceFileName, destinationFileName);
// The line below is actually faster and a lot less CPU-consuming
// File.Copy(sourceFileName, destinationFileName, true);
Console.ReadKey();
}
public static async void FileCopyAsync(string sourceFileName, string destinationFileName, int bufferSize = 0x1000, CancellationToken cancellationToken = default(CancellationToken))
{
using (var sourceFile = File.OpenRead(sourceFileName))
{
using (var destinationFile = File.OpenWrite(destinationFileName))
{
Console.WriteLine($"Copying {sourceFileName} to {destinationFileName}...");
await sourceFile.CopyToAsync(destinationFile, bufferSize, cancellationToken); …Run Code Online (Sandbox Code Playgroud) 我在读取网络驱动器上的大文件(~400 mb)时遇到一个有趣的问题。最初,我将完整的网络地址输入 FileInfo 中,并使用 CopyTo 函数将其传输到本地临时驱动器,然后读取它。这似乎工作正常,它不慢但也不快 - 只是嗯。CopyTo功能将使运行该程序的计算机的网络利用率始终保持在50%以上,这已经相当不错了。
为了加快这个过程,我尝试将网络文件直接读入内存流,以消除中间人。当我尝试这个(使用此处描述的异步复制模式)时,速度非常慢。我的网络利用率从未超过 2% - 几乎就像有什么东西在限制我。仅供参考,我在通过 Windows 资源管理器直接复制同一文件时观察了我的网络利用率,它达到了 80-90%...不知道这里发生了什么。下面是我使用的异步复制代码:
string line;
List<string> results = new List<string>();
Parser parser = new Parser(QuerySettings.SelectedFilters, QuerySettings.SearchTerms,
QuerySettings.ExcludedTerms, QuerySettings.HighlightedTerms);
byte[] ActiveBuffer = new byte[60 * 1024];
byte[] BackBuffer = new byte[60 * 1024];
byte[] WriteBuffer = new byte[60 * 1024];
MemoryStream memStream = new MemoryStream();
FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileSystemRights.Read, FileShare.None, 60 * 1024, FileOptions.SequentialScan);
int Readed = 0;
IAsyncResult ReadResult;
IAsyncResult WriteResult;
ReadResult = …Run Code Online (Sandbox Code Playgroud) 我想使用filestream将文件从一个文件夹复制到另一个文件夹。当我尝试使用file.copy时,我正在获取此文件被另一进程使用,以避免这种情况,我想使用C#。有人可以提供一个示例,用于将文件从一个文件夹复制到另一个文件夹吗?
c# ×4
.net ×3
asynchronous ×1
copying ×1
file-copying ×1
file-io ×1
filestream ×1
io ×1
stream ×1
winforms ×1