从文件流C#获取数据时出现问题 [英] Problem getting data from filestream C#

查看:176
本文介绍了从文件流C#获取数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我有一个加密文件,我正在尝试使用FileStream C#解密它。



我正在传递文件路径来创建文件流,当我尝试从FileStream中获取数据时,数据为空。



Dear All,

I have an encrypted file and I'm trying to decrypt it by using FileStream C#.

I'm passing the file path to create file-stream and when I'm trying to fetch the data from the FileStream, the data is null.

SampleCode
FileStream fs = new FileStream(@"" + offlinePath, FileMode.Create);
                    WriteToFile("converted to stream file: " + @"" + offlinePath);
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        WriteToFile("Reading file");
                        string data = sr.ReadToEnd(); //Getting empty
                        WriteToFile("Data: " + data);
                        offlineExamResult = Cryptography.Decrypt<OfflineExamResult>(data);
                        WriteToFile("Offline result file DECRYPTED"); //log file
                    }





我尝试过:





What I have tried:

public string UploadOfflineResult()
        {
            var pathSouce = Directory.GetParent(Directory.GetParent(Environment.SystemDirectory.ToString()).ToString()).FullName;
            var offlinePath = pathSouce +  ConfigurationManager.AppSettings["resultFile"];
            WriteToFile(offlinePath);
            FileInfo _file = new FileInfo(offlinePath);
            WriteToFile(_file.Exists ? "Offline result file exist" : "Offline result file does not exist");


            if (_file.Exists && getPing())
            {
                WriteToFile("Working ON offline exam result file");
                OfflineExamResult offlineExamResult = null;
                try
                {
                    FileStream fs = new FileStream(@"" + offlinePath, FileMode.Create);
                    WriteToFile("converted to stream file: " + @"" + offlinePath);
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        WriteToFile("Reading file");
                        string data = sr.ReadToEnd(); //Getting empty
                        WriteToFile("Data: " + data);
                        offlineExamResult = Cryptography.Decrypt<OfflineExamResult>(data);
                        WriteToFile("Offline result file DECRYPTED");
                    }
                }
                catch (Exception ex)
                {
                    ex = ex.InnerException; //Avoid warning
                    return JsonConvert.SerializeObject(
                        new { success = false, message = "Invalid result file content" },
                        Formatting.None,
                        new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
                }


                CommunicationHelper.UpdateCandidateOfflineExamResultUsingWindowService(JsonConvert.SerializeObject(offlineExamResult, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }));
                WriteToFile("UpdatedOfflineCandidateExamResult: CandidateSessionID=" + offlineExamResult.CandidateSessionID + " ");
                return true.ToString();


            }
            return false.ToString();
        }





任何人都可以帮助我。



谢谢



Can anyone please help me.

Thanks

推荐答案

创建FileStream时,你应该使用FileMode.Open而不是FileMode.Create,即

When creating your FileStream you should be using FileMode.Open and not FileMode.Create, i.e.
FileStream fs = new FileStream(@"" + offlinePath, FileMode.Open);

使用FileMode.Create将创建一个新文件(如果文件已存在则覆盖该文件),而FileMode.Open用于打开现有文件。



或者,您可以在创建StreamReader时将文件名作为参数传递,而根本不创建文件流。

Using FileMode.Create will create a new file (overwriting the file if it already exists) while FileMode.Open is used to open an existing file.

Alternatively, you could pass the file name as a parameter when creating the StreamReader and not create a file stream at all.

using (StreamReader sr = new StreamReader(@"" + offlinePath))


如果数据没有数据,则文件中没有任何内容可供阅读。
If data comes up with no data, there was nothing in the file to read.


这篇关于从文件流C#获取数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆