我需要使用File.OpenRead和File.OpenWrite方法将文件的内容写入另一个文件.我无法弄清楚如何做到这一点.
如何修改以下代码以便为我工作.
using (FileStream stream = File.OpenRead("C:\\file1.txt"))
using (FileStream writeStream = File.OpenWrite("D:\\file2.txt"))
{
BinaryReader reader = new BinaryReader(stream);
BinaryWriter writer = new BinaryWriter(writeStream);
writer.Write(reader.ReadBytes(stream.Length));
}
Run Code Online (Sandbox Code Playgroud) 你如何限制每秒的操作次数?
假设我们必须将文件从一个位置复制到另一个位置,并且我们不希望每秒处理超过5个文件.
请看看我在做什么
private static string currentStamp;
private static int processedInCurrentStamp = 0;
private static void Main(string[] args)
{
currentStamp = DateTime.Now.ToString("{0:d/M/yyyy HH:mm:ss}");
Run();
}
private static void Run()
{
for (int i = 0; i < Int32.MaxValue; i++)
{
string s = DateTime.Now.ToString("{0:d/M/yyyy HH:mm:ss}");
if (currentStamp.Equals(s))
{
if (processedInCurrentStamp < 5)
{
ProcessItem();
processedInCurrentStamp++;
}
}
else
{
Console.WriteLine("{0} ::: {1}", currentStamp, processedInCurrentStamp);
currentStamp = s;
processedInCurrentStamp = 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我需要一种更优雅和防弹的方式.