Ошибка CS0122. C# is inaccessible due to its protection level

309
15 ноября 2019, 05:30

Не могу понять почему не создаётся объект класса. Оба класса объявлены в одном пространстве имён. Объявление класса FileProcessing как public не дало результата.

using System;
using System.Text;
using System.Net.Sockets;
using System.IO;
namespace TextEditorServer
{
    class FileProcessing
    {
        private string filePath = Directory.GetCurrentDirectory() + @"\files";
        DirectoryInfo fileDirectory;
        FileProcessing()
        {
            fileDirectory = new DirectoryInfo(filePath);
            if (!fileDirectory.Exists) fileDirectory.Create();
        }
        public void CreateFile(string fileName, string data)
        {
            DirectoryInfo currentDirectory = new DirectoryInfo(filePath);
            if (!currentDirectory.Exists) currentDirectory.Create();
            filePath += @"\" + fileName;
            if (!File.Exists(filePath)) File.Create(filePath);
            using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8))
            {
                sw.WriteLine(data);
            }
        }
    }
    class ClientClass
    {
        private TcpClient client = null;
        private NetworkStream stream = null;
        private string command = null;
        private string fileName = null;
        private string data = null;
        private int requestRange = 0;
        public ClientClass(TcpClient tcpClient)
        {
            client = tcpClient;
        }
        public void Process()
        {
            string data = null;
            try
            {
                stream = client.GetStream();
                data = RecieveRequest();
                ExecuteRequest();
                Console.Write(data);
                SendResponse();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (stream != null)
                    stream.Close();
                if (client != null)
                    client.Close();
            }
        }
        private string RecieveRequest()
        {
            StringBuilder builder = new StringBuilder();
            byte[] buffer = new byte[64];
            int countBytes = 0;
            do
            {
                countBytes = stream.Read(buffer, 0, buffer.Length);
                builder.Append(Encoding.UTF8.GetString(buffer, 0, countBytes));
            }
            while (stream.DataAvailable);
            data = builder.ToString();
            DistructRequest();
            return data;
        }
        private void SendResponse()
        {
            byte[] buffer = new byte[64];
            buffer = Encoding.UTF8.GetBytes(data);
            stream.Write(buffer, 0, buffer.Length);
        }
        private void DistructRequest()
        {
            string dataCopy = data;
            string[] info = new string[5];
            dataCopy = dataCopy.TrimStart('<');
            info = dataCopy.Split('>');
            requestRange = int.Parse(info[0]);
            info[1] = info[1].TrimStart('<');
            command = info[1];
            info[2] = info[2].TrimStart('<');
            fileName = info[2];
            data = info[4];
        }
        void ExecuteRequest()
        {
            FileProcessing file = new FileProcessing();
            if (command == "connect")
            {
                file.CreateFile(fileName, data);
            }
        }
    }
}
Answer 1

В Вашем коде у этого класса private конструктор.

    public FileProcessing()
    {
        fileDirectory = new DirectoryInfo(filePath);
        if (!fileDirectory.Exists) fileDirectory.Create();
    }
READ ALSO
Разрешить inline-style по &ldquo;белому списку&rdquo;

Разрешить inline-style по “белому списку”

На своём сайте я использую Mathquill, который для корректного изображения встраивает стили в код страницыНапример:

106
Wordpress, вопрос-ответ [закрыт]

Wordpress, вопрос-ответ [закрыт]

Как можно сделать функционал, похожий на комментарии, но это будет вопрос-ответ вместо комментария? те

100
Ограничения вывода имени из БД

Ограничения вывода имени из БД

Подскажите как нажатием кнопки выводить имя из базы данных один раз, проблема в том что я вывожу имя но при втором ножати он опять выводит...

110
Вывод статей по годам и темам YII2

Вывод статей по годам и темам YII2

Есть статьи, которые имеют дату опубликованияЕсть связанная таблица с категориями статей

121