如何复制文件并覆盖现有文件。 [英] How to copy the file and overwrite the existing one.
问题描述
我有两个名为load和new的按钮。首先,您必须读取/加载文件。当您单击新按钮时,它必须复制您从加载按钮加载/读取的文件并覆盖它并显示修改日期。我怎么能这样做。我试过下面的代码,没有任何反应。
新按钮下的副本代码
FileInfo originalfile = new FileInfo(PathSelection); //这是w
FileInfo newfile = new FileInfo(PathSelection); //我想
保存按钮
I have two buttons called load and new. first you have to read/load the file. when you click new button it must copy the file you loaded/read from load button and overwrite it and shows date modified as well. how can i do that.i tried the code below and nothing happens.
code for copy under new button
FileInfo originalfile = new FileInfo(PathSelection); //this is w
FileInfo newfile = new FileInfo(PathSelection); //i want to
save button
private void btnSave_Click(object sender, EventArgs e)
{
saveFileDialog1.InitialDirectory = PathSelection;
saveFileDialog1.Title = "Save Resource Files";
saveFileDialog1.CheckPathExists = true;
saveFileDialog1.DefaultExt = "resx";
saveFileDialog1.Filter = "Save Resource Files (*.resx)|*.resx";
saveFileDialog1.RestoreDirectory = true;
PathSelection = saveFileDialog1.FileName;}
推荐答案
你的第一个问题是你没有显示对话框...你应该有类似
Your first issue is that you have not shown the dialog ... you should have something like
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
PathSelection = saveFileDialog1.FileName;
//Write to your file here - Filename will contain the fully pathed target
}
第二个问题是saveDialog实际上并没有为你保存文件 - 你必须这样做。
这可以像使用 File.WriteAllText
一样简单 - 它取决于你如何在另一个按钮下加载文件以及如何存储它在记忆中
The second problem is that the saveDialog doesn't actually save the file for you - you have to do that.
This could be as simple as using File.WriteAllText
- it depends on how you've loaded the file under the other button and how you are storing it in memory
这篇关于如何复制文件并覆盖现有文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!