【问题标题】:how to save and open your own file C# [closed]如何保存和打开自己的文件 C# [关闭]
【发布时间】:2020-04-14 18:01:49
【问题描述】:

当我在我的程序中按下保存按钮时,我想用我自己的扩展名保存一个文件。 (例如.xyz

那我想打开它,通过这个程序读取里面的数据。

我怎样才能做到这一点?

【问题讨论】:

标签: c# file-writing file-read


【解决方案1】:

当我在我的程序中按下保存按钮时,我想用我自己的扩展名保存一个文件。 (例如.xyz)。 执行以下操作:

const string directory = @"D:\Temp"; // this can be any location you want

if (!Directory.Exists(directory))
    Directory.CreateDirectory(directory);

string path = Path.Combine(directory, "Test.custom");
if (!File.Exists(path))
{
    File.Create(path);

    // other logic
}

那我想打开它,通过这个程序读取里面的数据。

string textFile = @"D:\Torrent\Test.custom";

using StreamReader sr = new StreamReader(textFile);
var line = sr.ReadToEnd();
Console.WriteLine(line);

如果您想稍后在文件中保存某些内容,最简单的方法是:

const string textFile = @"D:\Torrent\Test.custom";
string createText = $"Some text {Environment.NewLine}";
File.WriteAllText(textFile, createText);

【讨论】:

  • 太好了,现在我有一个缺点。这个里面的数据怎么写。
  • @İdrisBölge 我已经更新了帖子,请检查。
  • 谢谢:D 它有效,但我怎样才能使它无法用记事本阅读..
  • @İdrisBölge 通过记事本您可以尝试打开几乎任何文件。因此,除非卸载记事本,否则我不知道如何阻止任何人以这种方式阅读您的文件;)
  • 无论如何,谢谢。我最好稍后将其作为帖子打开。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
相关资源
最近更新 更多