1

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.

4
  • If you need a dynamic length, use a List not an array. Commented Apr 13, 2015 at 14:16
  • GetDirectories returns an array of the subdirectories in the path. GetFiles returns an array of the actual files in the path. Is that what you were looking for? Your question is a little unclear. Commented Apr 13, 2015 at 14:34
  • I have followed this link for files. msdn.microsoft.com/en-us/library/c1sez4sc%28v=vs.110%29.aspx..... But i need to read all files name in an string array. Commented Apr 13, 2015 at 14:42
  • @VarunJi I realize you already accepted an answer but if you take a look at my answer it explains the areas covered a bit further. Commented Apr 13, 2015 at 14:56

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

9 Comments

You cannot dynamically add to an array necessarily but you can re-size an array with Array.Resize (ref array, int, etc.). So saying you must create a new array is not necessarily correct.
@CalebB - "This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one" - MSDN
@CalebB it is in the very nature of arrays, that they can not be dynamically resized. there is no alternative to creating a bigger/smaller one and copying the content - whether you do it in your domain or use the frameworks possibilities.
@CalebB I don't want to look like schoolmasterly, but: It is not about variables, it is about instances/types in general. This said, creating a new instance is mandatory when resizing arrays, and not dependent on the variables capturing the instances.
@CalebB I think we went off track - both. period. your intial comment, saying So saying you must create a new array is not necessarily correct., is wrong - by the simple reasons of how arrays work.
|
1

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().

Comments

1

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().

2 Comments

Thanks CalebB. But I have to traverse through the Directories and have to capture all files. So I have used the link mentioned above. And I think your answer is quiet close to my question as well. Thanks again for your help.
You can easily make it recursive by using directory.GetFiles("*", SearchOptions.AllDirectories) or just put it in a recursive method. You can also make it faster by adding .AsParallel() with this format.
0

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.

3 Comments

The static Directory.GetDirectories(string) returns an array of strings. DirectoryInfo.GetDirectories() returns an array of DirectoryInfo not string and even when using the Static method it returns an array of string which is the path to sub-directories not files.
The MSDN article he linked to says it returns a string array. So I based my answer off that. I had kind of a difficult time properly understanding his question- if he wants files then why is he using GetDirectories?
Yes the question is slightly vague, the linked article is that of the Static Directory.GetDirectories(string) method that I mentioned above. He may be misunderstanding the returned value, thinking it is returning the paths of the content of the folder vs. subdirectories.
0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.