【问题标题】:overwrite option file to user?将选项文件覆盖给用户?
【发布时间】:2011-04-20 15:13:22
【问题描述】:

我需要在目录或路径中创建一个二进制文件,如果文件存在,应用程序应该询问用户是否要覆盖它。我有这段代码来编写文件,那么,如何验证文件是否存在并显示以控制台消息?

using (FileStream fileStream = new FileStream(binaryFilePath, FileMode.Create)) // destiny file directory.
            {
                using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
                {
                    for (int i = 0; i < frameCodes.Count; i++)
                    {
                        binaryWriter.Write(frameCodes[i]);
                    }
                }
            }

谢谢..

【问题讨论】:

    标签: c# filestream binaryfiles


    【解决方案1】:

    File.Exists(binaryFilePath) 应该可以帮到你。

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

      if(File.Exists(binaryFilePath) && PromptUser())
      {
      ...
      }
      

      PromptUser() 会询问用户是否要覆盖文件

      【讨论】:

      • 啊,我肯定跳过了控制台部分。
      猜你喜欢
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多