Вот файл: < /p>
Код: Выделить всё
using System;
using System.Security.Cryptography; //honestly don't know why this is here
using FileManagement; // I'm going to talk about this later
namespace P2C2._1;
public class ConditionalHello
{
public static void SayHello(string receiver)
{
Console.WriteLine("Hello " + receiver + " !");
}
public static void AgeCondition(string receiver)
{
Console.WriteLine("How old are you?");
int age = Convert.ToInt32(Console.ReadLine());
if (age 8)
{
SayHello(receiver);
}
}
public static void Main(string[] args)
{
// main function, say hello if you are old enough
Console.Write("Enter the receiver's name : ");
string receiver = Console.ReadLine();
AgeCondition(receiver);
// (temp lines)
// appending the README from OpenClassRoom to explain the transformation of P2C2.1
// 1. content creation
string content = "# Originally an OpenClassRoom file, from their course 'debuting w\ C#" +
"***" +
"I transformed it to add an age condition to cast away the children under 8" +
"Next, I need to add a function for when the age entered isn't a number.";
// 2. appending the file
string fileName = "README.md";
WriteFile(content, fileName); //error CS0103 right here
}
}
// adding text to the og README file
namespace FileManagement;
public class FileManagement
{
public static void WriteFile(string content, string fileName)
{
try
{
string filePath = @"C:\Users\(lots of folders)\P2C2.1\README.md";
File.AppendAllText(filePath, Environment.NewLine + content);
Console.WriteLine("Writing done in the file" + fileName);
string fullPath = Path.GetFullPath(fileName);
Console.WriteLine($"You can find the file in: {fullPath}");
}
catch (UnauthorizedAccessException e) //there might be too much catch here
{
Console.WriteLine("Non-authorized access: " + e.Message);
}
catch (IOException e)
{
Console.WriteLine("E/S error: " + e.Message);
}
catch (Exception e)
{
Console.WriteLine($"Error while writing in the file: {e.Message}");
}
}
}
< /code>
Почему я не могу просто инициировать файл записи в своей основной функции, когда я вкладываю «статический» перед методом записи? /> [*] Скопировать/вставить пространство имен и файловая файл метода, чтобы предотвратить Tipo;
[*] Прочитайте документацию Microsoft здесь, в которой говорилось, что это может произойти, если метод определяется в Try/Catch, но я думаю, что я не могу сделать иначе в моем случае; Я ничего не понял.
Подробнее здесь: https://stackoverflow.com/questions/793 ... when-the-n