-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.cs
55 lines (47 loc) · 1.74 KB
/
debug.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.IO;
public class PathDebug
{
public static string GetFullPath()
{
// Print the current working directory
Console.WriteLine("Current Directory: " + Environment.CurrentDirectory);
// Set your relative path starting from the 'temp' folder
string relativePath = Path.Combine("temp", "MyText.txt");
// Combine it with the current working directory
string fullPath = Path.Combine(Environment.CurrentDirectory, relativePath);
// Get the directory path
string? directoryPath = Path.GetDirectoryName(fullPath);
// Handle the potential null value from GetDirectoryName
if (directoryPath == null)
{
Console.WriteLine("Error: Could not determine the directory path.");
return string.Empty; // Exit the program as the path is invalid
}
// Check if the directory exists, create it if it doesn't
if (!Directory.Exists(directoryPath))
{
Console.WriteLine("Directory does not exist. Creating directory...");
Directory.CreateDirectory(directoryPath);
}
else
{
Console.WriteLine("Directory exists.");
}
// Check if the file exists
if (!File.Exists(fullPath))
{
Console.WriteLine($"File '{fullPath}' does not exist. Creating the file...");
using (StreamWriter sw = File.CreateText(fullPath))
{
sw.WriteLine("This is a test file created by the program.");
}
}
else
{
Console.WriteLine($"File '{fullPath}' already exists.");
}
// Return the full path so Program.cs can use it
return fullPath;
}
}