0
var temp = toCheck.GetData(DataFormats.FileDrop);

I have the code above in my program. toCheck is an IDataObjecct containing a file(image to be specific) path.

When I debug, I see the value stored under as such:
temp -> {string[1]}
[0] -> "C:\....rest of path"

Everything is correct but when I try to access the string inside, I cannot. If I use the toString() method it returns System.String[] but not the filepath.
Any suggestions on how I can retrieve the filepath inside of the variable?

3 Answers3

2

temp -> {string[1]} Means that temp is an array of type string with one element.

[0] -> "C:\....rest of path" Means that the element at index 0 is equal to the path.

Since GetData returns the string array boxed as an object, you'll also have to unbox it first. Then you can simply access that element like this:

string[] strArr = temp as string[];
string s = temp[0];
Foggzie
  • 9,691
  • 1
  • 31
  • 48
  • Thank you Gunther, I should have mentioned that I tried this. When I set string s = temp[0]; I get the following error: Cannot apply indexing with [] to an expression of type 'object' This is where my confusion comes from. I thought it would be temp[] to access the string but it appears it's not that easy. – user4238148 Dec 11 '15 at 15:47
  • @user4238148 Woops! Forgot about the unboxing of the `object`. I updated the answer. – Foggzie Dec 11 '15 at 16:16
1

temp is an array of strings, that is why you are getting that. If you want to get the first string of the array then:

var mystring = temp[0];

That should do

Also you can check if the string[] has any string with:

if(temp.Length > 0) myString = temp[0];

where myString is a variable of type string

Ignacio Gómez
  • 1,587
  • 4
  • 23
  • 41
0

I found the answer!

string[] arr = ((IEnumerable)temp).Cast<object>().Select(x => x.ToString()).ToArray();

Using the above statement you can go from type object to an array of string.
You need to have included using.System.Linq for it to work.

 var temp = toCheck.GetData(DataFormats.FileDrop);
        string[] arr = ((IEnumerable)temp).Cast<object>().Select(x => x.ToString()).ToArray();
        string path = "";

        foreach(string a in arr)
            path = a;

This ended up being all the code if anybody is curious. I iterate over the array because otherwise it would skip the rest of the function when I debugged. Not sure what was causing this but this works. Thank you all for your help!