C
C#4mo ago
zzz

reading files from a directory

using System.IO;

namespace Program
{
class program
{
public static void FileLoader()
{
string[] filePaths = Directory.GetFiles(@"C:\Users\joesm\Source\Repos\441101-2324-digital-portfolio-jsd6666\Summative 4\", "*.mark", SearchOption.TopDirectoryOnly);
for (int i = 0; i < filePaths.Length; i++)
{
Console.WriteLine($"{i + 1}. {filePaths[i]}");
}
int fileSelect = int.Parse(Console.ReadLine());
}
public static void Main()
{
Console.WriteLine("Please select a file to load.");
FileLoader();
}
}
}
using System.IO;

namespace Program
{
class program
{
public static void FileLoader()
{
string[] filePaths = Directory.GetFiles(@"C:\Users\joesm\Source\Repos\441101-2324-digital-portfolio-jsd6666\Summative 4\", "*.mark", SearchOption.TopDirectoryOnly);
for (int i = 0; i < filePaths.Length; i++)
{
Console.WriteLine($"{i + 1}. {filePaths[i]}");
}
int fileSelect = int.Parse(Console.ReadLine());
}
public static void Main()
{
Console.WriteLine("Please select a file to load.");
FileLoader();
}
}
}
its showing me the file names with the directory included, i just want it to display the file name on each line without the directory??
No description
12 Replies
Angius
Angius4mo ago
Split the path by \ and get just the last segment That'll be the easiest way
MODiX
MODiX4mo ago
Angius
REPL Result: Success
var path = @"C:\Foo\Bar\Baz\My Cool Files\File.png";
var segments = path.Split('\\');
segments[^1]
var path = @"C:\Foo\Bar\Baz\My Cool Files\File.png";
var segments = path.Split('\\');
segments[^1]
Result: string
File.png
File.png
Compile: 387.446ms | Execution: 31.870ms | React with ❌ to remove this embed.
Jimmacle
Jimmacle4mo ago
no need to implement the splitting yourself
mtreit
mtreit4mo ago
Use DirectoryInfo and FileInfo types instead for this kind of thing.
var directory = new DirectoryInfo(@"c:\temp");
var files = directory.GetFiles();
foreach (var file in files)
{
Console.WriteLine(file.Name);
}
var directory = new DirectoryInfo(@"c:\temp");
var files = directory.GetFiles();
foreach (var file in files)
{
Console.WriteLine(file.Name);
}
zzz
zzz4mo ago
i understand the use for 1 file but i dont understand how i can use iterations to get all file name s in a directory
Angius
Angius4mo ago
You get all the file paths and, in a loop, extract the file names from them
mtreit
mtreit4mo ago
The code I showed does exactly that.
zzz
zzz4mo ago
can you show me bro i dont want to just copy paste this code i dont understand ive just been staring at this shit for an hour trying not to punch a wall
Jimmacle
Jimmacle4mo ago
so you already have a loop with the absolute path to the file in filePaths[i] pick either Path.GetFileName or manually splitting and use that to get just the name or rewrite the whole thing based on mtreit's example
zzz
zzz4mo ago
public static void FileSelecter()
{
string rootPath = @"C:\Users\joesm\Source\Repos\441101-2324-digital-portfolio-jsd6666\Summative 4";
var files = Directory.GetFiles(rootPath, "*.mark", SearchOption.TopDirectoryOnly);
int count = 1;
foreach (string file in files)
{
Console.WriteLine($"{count}. {Path.GetFileName(file)}");
count = count + 1;
}
int fileSelect = int.Parse(Console.ReadLine());
FileLoader();
}
public static void FileSelecter()
{
string rootPath = @"C:\Users\joesm\Source\Repos\441101-2324-digital-portfolio-jsd6666\Summative 4";
var files = Directory.GetFiles(rootPath, "*.mark", SearchOption.TopDirectoryOnly);
int count = 1;
foreach (string file in files)
{
Console.WriteLine($"{count}. {Path.GetFileName(file)}");
count = count + 1;
}
int fileSelect = int.Parse(Console.ReadLine());
FileLoader();
}
so i got it to work but it doesnt work like before as i have no array of file names which is what i need to access the file selected
Angius
Angius4mo ago
files is the array that stores full file paths If you want to have a collection of just the file names, you can also use the loop for that Create a new List<string> that would store the names alone And, in the loop, for each path you have, add just the filename to that list Alternatively, you can use LINQ method .Select()
Want results from more Discord servers?
Add your server
More Posts