File name from a string with file path C# [duplicate] - c#

This question already has answers here:
Given a filesystem path, is there a shorter way to extract the filename without its extension?
(10 answers)
Closed 10 months ago.
Suppose I have an array of strings with full file names and paths. For example
string[] filesArray = Directory.GetFiles(#"C:\dir", "*", SearchOption.AllDirectories);
Now let's say we have the following data in the array:
filesArray[0] = "C:\dir\file1.txt"
filesArray[1] = "C:\dir\subdir1\file2.txt"
filesArrat[2] = "C:\dir\subdir1\subdir2\file3.txt"
... etc.
Now I want a new array, that will store only the files' names, something like this:
nameArray[0] = "file1.txt"
nameArray[1] = "file2.txt"
nameArray[2] = "file3.txt"
What is the best way to do it, using string array only, without storing the full FileInfo class objects?

Using LINQ it's pretty simple
string[] nameArray = filesArray.Select(p => Path.GetFileName(p)).ToArray()

Related

How do I get the file name with it's extension? [duplicate]

This question already has answers here:
Given a filesystem path, is there a shorter way to extract the filename without its extension?
(10 answers)
Closed 7 years ago.
I have tried this but this only gets the file name not the extension. How do I get file name with extension?
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
string filename = Path.GetFileNameWithoutExtension(FileList[0]);
Path.GetFileName()
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
string filename = Path.GetFileName(FileList[0]);
Check the documentation

Splitting string in c# by delimiter [duplicate]

This question already has answers here:
How to convert string[] to ArrayList?
(4 answers)
Closed 7 years ago.
I have a string "foo|bar|time||||etc|"
I want to loop through each entry and save them one by one in a ArrayList. The delimiter is '|'.
For example, the ArrayList should contain:
"foo"
"bar"
"date"
""
""
""
"etc"
""
How do I save each string in an ArrayList?
You can easily Split your string with | and use ToList method like;
var s = "foo|bar|time||||etc|";
var list = s.Split('|').ToList();
And it's almost 2016. Don't use ArrayList anymore. This structure belongs on old days when C# doesn't have Generics. Use List<T> instead.
use string.Split and ToList
string str = "foo|bar|time||||etc|";
List<string> words = str.Split('|').ToList(); //a lot better than ArrayList

Writing contents of a class to a file [duplicate]

This question already has answers here:
Best practices for serializing objects to a custom string format for use in an output file
(8 answers)
Closed 7 years ago.
I have a class, all string properties like this:
public class MyClass
{
public string Name {get;set;}
public string Phone {get;set;}
// a bunch of more fields....
{
And a list of that class List<MyClass> myListOfObjects; that I have populated it through out the program with values.
And then I have a file (csv) with headers:
Name, Phone, etc...
I want to write the contents of that myListOfObjects into that file.
I was thinking just loop through them and write one row per object. But wanted to see is there a better nicer way?
You can write all your data in one shot, like
var list = new List<MyClass>();
var fileData = list.Select(row => string.Join(",", row.Name, row.Phone, row.Etc)).ToArray();
File.WriteAllLines(#"C:\YourFile", fileData);
Note: This is one way to improve the file write, but it doesn't handle un-escaped text data like Name with comma.

How do I make a string array based on a text file with newline delimited entries in C#? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# how to convert File.ReadLines into string array?
I have a text file that I want to make a String array from. The file is newline delimited. For example,
entry1
entry2
entry3
will make an array of {"entry1", "entry2", "entry3"}
EDIT: I am wanting to do this using DownloadString in WebClient
So you're just trying to split a string into an array that is delimited with a new line?
If so, this should work:
string temp = webClient.DownloadString(someurl);
string[] lines = temp.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
Good luck.

Return filename without extension from full path in C# [duplicate]

This question already has answers here:
Getting file names without extensions
(14 answers)
Closed 7 years ago.
I'm looking for a way to return fileName from full path but without extension.
private static string ReturnFileNameWithoutExtension(string varFullPathToFile) {
string fileName = new FileInfo(varFullPathToFile).Name;
string extension = new FileInfo(varFullPathToFile).Extension;
return fileName.Replace(extension, "");
}
Is there more bullet proof solution then replacing extension with empty string?
return Path.GetFileNameWithoutExtension (fullpath);
i'm using System.IO.Path.GetFileNameWithoutExtension(filename);
You're looking for Path.GetFileNameWithoutExtension
One More solution
string fileName = new FileInfo(varFullPathToFile).Name;
fileName=fileName.Substring(0, fileName.LastIndexOf("."));

Categories

Resources