无法将字符串转换为System.IO.Stream

时间:2017-06-29 04:44:36

标签: c# data-stream

在这个C#项目中,我有一个JSON文件,我将数据序列化到。据我所知,您创建了一个StreamWriter并将该流编写器用作参数。但是,当我使用JSON文件的文件路径作为参数创建流编写器时,会给我一个错误:Argument 1: Cannot Convert String to System.IO.Stream

这是文件

using Newtonsoft.Json;
using ProjectObjects;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System;

namespace level2
{
    public static class Data
    {
    static string selectedTeamName,selectedPlayerName;
    static Team selectedTeam = new Team("unknown");
    static Player selectedPlayer = new Player(" ");
    static List<Team> AllTeams = new List<Team>();


    public static void LoadFromCSV(string name, CSVType type)
    {
        string directory = Directory.GetCurrentDirectory();
        var dirinfo = new DirectoryInfo(directory);
        var MaindataFileDir = Path.Combine(dirinfo.FullName, "N/A");
        var file = new FileInfo(MaindataFileDir);

        if (type.Equals(CSVType.BJA))
        {

        }else if (type.Equals(CSVType.DefenseStats))
        {

        }else if (type.Equals(CSVType.HitStat))
        {

        }
    }

    public static void SerializeTeam(string filename)
    {
        string directory = Directory.GetCurrentDirectory();
        var dirinfo = new DirectoryInfo(directory);
        var MaindataFileDir = Path.Combine(dirinfo.FullName, "/Data/Datar.json");
        var file = new FileInfo(MaindataFileDir);
        string filepath = directory + "/Data/Datar.json";




        if (file.Exists)
        {
            var serializer = new JsonSerializer();
            using (var writer = new StreamWriter(MaindataFileDir))
            using (var textwriter = new JsonTextWriter(writer))
            {
                //serializer.Serialize(textwriter, AllTeams);not finished
            }
        }

    }

    public static void DeserializeTeam()
    {
        string directory = Directory.GetCurrentDirectory();
        var dirinfo = new DirectoryInfo(directory);
        var MaindataFileDir = Path.Combine(dirinfo.FullName, "/Data/Datar.json");
        var file = new FileInfo(MaindataFileDir);

        if (file.Exists)
        {
            var serializer = new JsonSerializer();
            using (var writer = new StreamReader(MaindataFileDir))
            using (var textreader = new JsonTextReader(writer))
            {
                //serializer.Deserialize(textreader, AllTeams);not finished
            }
        }


    }
}

  }

我很难理解问题的原因是在MSDN上有一个与我正在做的非常类似的例子。

来自Here:

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";

            using (StreamWriter writer = new StreamWriter(fileName))
            {
            writer.Write(textToAdd);
            }
        }
    }
}

如果它很重要,我使用的是Microsoft Visual Studio 2017社区。 原谅我糟糕的格式。 先感谢您。

0 个答案:

没有答案