【问题标题】:I can't not seem to successfully overwrite and some other things我似乎无法成功覆盖和其他一些事情
【发布时间】:2013-07-29 23:49:01
【问题描述】:

所以,基本上我已经创建了这个程序,它将选定的文件复制到公司在 Roaming AppData 文件夹中的命名文件夹中,这还不错。我的意思是,现在的设置还不错,但我希望对它有更多的控制权。

string fullFileName = item.FileName;
string fileNameWithExt = Path.GetFileName(fullFileName);
string destPath = Path.Combine(Application.UserAppDataPath, fileNameWithExt);
File.Copy(item.FileName, destPath);

在程序开始时,它会检查自定义 AppData 文件夹是否存在。

public void checkADfolder()
{

    string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

    string specificFolder = Path.Combine(folder, ".program");

    if (!Directory.Exists(specificFolder))
       Directory.CreateDirectory(specificFolder);
}

我希望文件转到 AppData 文件夹 .program 而不是带有应用程序公司名称的文件夹。

另外,我遇​​到了这个问题。当用户选择一个文件,然后关闭程序并再次打开程序并碰巧选择了相同的文件时,它会给出错误,因为该文件已经存在。 I need to have it overwrite all other files when the same file is selected.

去死吧,我也会问这个我为什么在这里。 我需要用户选择的文件来替换 AppData 文件夹中的另一个文件并重命名用户选择的文件。

基本上。用户选择文件。文件名为“user.txt”。现在它在 Roaming > .program > user.txt 我需要那个文件来替换一个文件,比如说它叫做“guest.txt”。它位于漫游 > .user > guest.txt 我需要将“user.txt”复制并重命名为“guest.txt”,然后替换.user 文件夹中的“guest.txt”。 我希望这能很好地解释一切。

我想我会将所有内容都放在一个帖子中,而不是制作多个帖子。 我已经搜索过,但似乎找不到任何答案:/

【问题讨论】:

  • 你用谷歌搜索过复制文件吗?并重命名文件?

标签: c# replace copy rename overwrite


【解决方案1】:

要覆盖文件,你只需要使用这里的方法:

File.Copy Method (String, String, Boolean);

要覆盖您的其他文件,只需再次调用该函数,如下所示:

File.Copy(@".\Roaming\program\user.txt", @".\Roaming\user\guest.txt", true);

【讨论】:

  • 好的,谢谢,现在我需要让第一个变量访问用户选择的文件。我是否能够将用户选择的文件存储为变量或其他东西,以便我能够更轻松地使用它?
  • 当然这是为文件构建的类:msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx 如果您有时间,您可以将答案标记为已接受吗?谢谢。
  • 谢谢你,马修。编码愉快。
  • 我无法让代码工作。废话,这是我的新代码code string fullFileName = item.FileName; FileInfo userSelected = new FileInfo(fullFileName); string fileNameWithExt = Path.GetFileName(fullFileName);字符串 destPath = Path.Combine(Application.UserAppDataPath, fileNameWithExt); File.Copy(item.FileName, destPath); ` 对吗?另外,不客气
  • 我诚实的建议是在调试器中尝试,单步调试代码,看看它是否有效。如果您遇到无法解决的问题,请在网站上发布另一个问题,希望有人能够帮助您解决新问题。祝你有美好的一天。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 2021-12-23
相关资源
最近更新 更多