【问题标题】:File not saving and there's no errors - Answer at bottom文件未保存且没有错误 - 答案在底部
【发布时间】:2018-04-04 17:23:38
【问题描述】:

我今天开始了一个随机项目,看看我是否可以制作自己的视频播放器,使用我制作的文件类型。除了从一组图像部分创建文件之外,一切都有效。下面是代码:

private void createBtn_Click(object sender, EventArgs e) {
    VideoProcessing processing = new VideoProcessing();

    List<Frame> frms = new List<Frame>();

    string[] filePaths = { };
    using (var fbd = new FolderBrowserDialog()) {
        DialogResult result = fbd.ShowDialog();

        if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath)) {
            filePaths = Directory.GetFiles(fbd.SelectedPath);
        }
    }

    foreach (String s in filePaths) {
        frms.Add(new Frame(ImageToByteArray(Image.FromFile(s))));
    }

    Frame[] f = frms.ToArray();

    string file = "";
    using (var fbd = new FolderBrowserDialog()) {
        DialogResult result = fbd.ShowDialog();

        if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath)) {
            file = fbd.SelectedPath + "Video2.frf";
        }
    }

    VideoFile video = new VideoFile();
    video.frames = f;
    video.name = file;

    processing.WriteToBinaryFile<VideoFile>(file, video);
}

我已尝试调试以查看发生了什么。但是,一切似乎都很正常,代码行也在运行,文件没有被保存,也没有抛出任何错误。

这是 VideoProcessing 类

 public class VideoProcessing {
    private Form1 frm = new Form1();

    public void WriteToBinaryFile<T>(string filePath, T objectToWrite, bool append = false) {
        using (Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create)) {
            var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            binaryFormatter.Serialize(stream, objectToWrite);
        }
    }

    public T ReadFromBinaryFile<T>(string filePath) {
        using (Stream stream = File.Open(filePath, FileMode.Open)) {
            var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            return (T)binaryFormatter.Deserialize(stream);
        }
    }

    public VideoFile CreateFile(string prePath) {
        VideoFile file = new VideoFile();
        int i = 0;
        file.frames = new Frame[frm.frames.Count];
        file.name = prePath;
        foreach (Frame frame in frm.frames) {
            file.frames[i] = frame;
            i++;
        }

        return file;
    }
}

[System.Serializable]
public class VideoFile {
    public string name;
    public string extension = ".frf";
    public Frame[] frames;
}

回答:

答案很简单,我设法错过了。感谢:Daisy Shipton 在下面的答案中我能够找到它:

在他的行中:file = fbd.SelectedPath + "Video2.frf";

我忘了在文件名前加斜杠,所以:

file = fbd.SelectedPath + "/Video2.frf";

我会坚持下去,希望像我这样愚蠢的人会觉得它有帮助!

【问题讨论】:

  • 这里的VideoProcessing 是什么?是你自己的类型吗?听起来这可能是问题所在。
  • 我将那个类添加到上面的代码中。这些文件是在我构建它之前保存的,所以没有添加它,抱歉。
  • 看来这是我们需要看到的实现,而不是所有其他代码:processing.WriteToBinaryFile&lt;VideoFile&gt;(file, video);
  • 刚刚添加了 Rufus。
  • 首先,我肯定会尽量避免.NET 二进制序列化。除此之外,它绝对不会创建您可以在其他任何地方播放的视频文件。您绝对确定您正在查看代码保存到的同一目录吗?如果你在调试器中运行它并在WriteToBinaryFile 中放置一个断点,然后检查file 的值,看起来对吗?对此最简单的解释是,它创建的文件没有问题,只是不在您期望的位置。

标签: c# file serialization saving-data


【解决方案1】:

使用Path.Combine 来避免该问题,因为它会自动添加缺少的斜线

【讨论】:

    猜你喜欢
    • 2011-04-29
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2013-12-16
    • 2015-09-19
    相关资源
    最近更新 更多