Reputation: 21
begginer here :). So i want to fill an array or list with results from a foreach loop.
Noob example
foreach (var drive in mounted_drives)
{
//asigning new path where to look for the UNCpath
string getUNC = path + "\\" + drive;
reg2 = Registry.CurrentUser.OpenSubKey(getUNC);
string UNCPath = reg2.GetValue("RemotePath").ToString(); //getting UNC PATH
Console.WriteLine(UNCPath);
}
so here i want each UNCPath to be saved to outside array or list that i can use later to write it in a file.
Dont wanna spill here my ideas since im not that deep into C# and .NET yet..
It may be simple but im stuck -.-
Thanks in advance
Upvotes: 0
Views: 340
Reputation: 8004
So i want to fill an array or list with results from a foreach loop. I want each UNCPath to be saved to outside array or list that I can use later.
There are other answer that address this already, but I have a different approach to this and a few suggestions to improve your current code.
The first suggestion is don't concatenate strings like you are:
string getUNC = path + "\\" + drive;
Look into the Path.Combine Method to do this for you.
Secondly you should always release resources when you can. You are opening up registry keys which means we should also always close and dispose of them.
Below is a static class with an extension routine. The routine returns an IEnumerable<string>
, this way it can defer execution until you actually need it. This is helpful considering you mentioned you want to use it later as a List<string>
and or Array
.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Win32;
public static class RegistryHelper
{
public static IEnumerable<string> GetRemotePaths(this IEnumerable<string> drives, string path)
{
if (drives == null || drives.Count() == 0 || string.IsNullOrEmpty(path))
yield break;
foreach (string drive in drives)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(Path.Combine(path, drive)))
{
if (key != null && key.GetValue("RemotePath") != null)
{
yield return key.GetValue("RemotePath").ToString();
key.Close();
}
}
}
}
}
Here's an example of usage for this extension routine:
var ienum = mounted_drives.GetRemotePaths(YOURPATHHERE); // Make sure to put your path in - delayed execution until you actually need it
var lstPaths = mounted_drives.GetRemotePaths(YOURPATHHERE).ToList(); // Make sure to put your path in - converts the return to a `List<string>`
var arrPaths = mounted_drives.GetRemotePaths(YOURPATHHERE).ToArray(); // Make sure to put your path in - converts the return to an array of strings
Upvotes: 1
Reputation: 130
You can just create a List outside the foreach and then appending what you want inside it.
var list = new List<string>();
list.Add("my string");
You can also use the Select method:
var list = mounted_drives.Select(e => {
string getUNC = path + "\\" + drive;
reg2 = Registry.CurrentUser.OpenSubKey(getUNC);
string UNCPath = reg2.GetValue("RemotePath").ToString(); //getting UNC PATH
return UNCPath;
}).ToList();
Upvotes: 0
Reputation: 453
You can try this:
List<string> mylist = new List<string>();
foreach (var drive in mounted_drives)
{
string getUNC = path + "\\" + drive;
reg2 = Registry.CurrentUser.OpenSubKey(getUNC);
string UNCPath = reg2.GetValue("RemotePath").ToString(); //getting UNC PATH
mylist.Add(UNCPath);
}
In case you should need to have an array instead of a list, you can use the method ToArray();
string[] myarray = mylist.ToArray();
Upvotes: 1