VarunJi
VarunJi

Reputation: 694

insert string in string[] array in C#

I have one issue with string[] array. I don't know how to insert string in string[]. I can not assign any array to it. But I need to insert string according to the requirement. So this has a dynamic length. I am new in C#, so i have no idea. So please let me know, how to set this?

I have some no. of files in some dynamic folders and i want to insert those names in string[] array. So I have created logic to read file sin directory from here. Now i want to insert these file names one by one.

Upvotes: 1

Views: 3464

Answers (5)

Nandha Kumar
Nandha Kumar

Reputation: 126

On a all-in-one solution this can be summarized as follows Note: As referred above Arrays are of fixed length hence their length can't be dynamically included

// Include IO, Linq namespace
using System.IO;
using System.Linq;

// To search files in a directory and collect in a collection
var directoryPath = "";
var fileNameList = Directory.GetFiles(directoryPath,"*.*", SearchOption.AllDirectories).ToList().Select(e=>Path.GetFileName(e));

// Casting the List to Array using ToArray()
var fileNameArray = fileNameList.ToArray();

Upvotes: 0

CalebB
CalebB

Reputation: 607

You have a few options here. First a clarification of the method you are using. Directory.GetDirectories(string) returns an array of string containing the paths of folders contained within the folder at the path passed to the method. This array does not contain file paths or file names. You can use an instance of DirectoryInfo such as:

DirectoryInfo directory = new DirectoryInfo("my path to directory");

DirectoryInfo[] subDirectories = directory.GetDirectories();

This will return your sub-directories contained within the directory at the path you used in the DirectoryInfo constructor.

As for arrays in general; you cannot add an element to an array as an array has a fixed length, however there is a method within the static Array class Array.Resize(ref yourArray, int newSize) that you may use to re-size then insert the new values at necessary indexes (this does create a new array from the backend and assign it the specified length and values from the previous array but in this case you don't need to keep track of multiple array variables within the user code, helping to keep it slightly easier to remember in my opinion) but if you are going to be doing this regularly you may be better off with using another Type such as List.

If you are wanting the file names such as myTextFile.txt and not the full paths you can use a little bit of Linq such as:

DirectoryInfo directory = new DirectoryInfo("my path to directory");

List<string> myFileNames = directory.GetFiles().Select(c => c.Name).ToList();

This will return a List of string, the names of the files within the directory at the path you used within your DirectoryInfo constructor.

You can do the same thing with getting directory names by swapping out the directory.GetFiles() for directory.GetDirectories() then appending the Select() and ToList().

Upvotes: 1

sab669
sab669

Reputation: 4104

GetDirectories already returns an array of strings. So since you don't really need to know how big the array is, you should just be able to do

string[] myVariable = dir.GetDirectories();

Then myVariable will be the same length as the number of subdirectories in the directory. Note though, that since arrays are fixed-length, you won't be able to add more to this array.

Upvotes: 0

Jon Tirjan
Jon Tirjan

Reputation: 3694

Arrays cannot be dynamically added to. You would need to create a new array and copy into it.

If you need to dynamically add to a collection, you're better off using a List instead of an array.

Upvotes: 5

Sajeetharan
Sajeetharan

Reputation: 222722

You can't add items to an array because array has fixed length, alternatively you can go for List<string>

To turn List To Array, You could just call list.ToArray().

Upvotes: 1

Related Questions