Check extension

S

shapper

Hello,

I am creating a method that given a path and a list of file types
(doc, docx, pdf, ...) checks if the file is of one of those types. I
am using:

public static bool FileType(string path, string[] types) {

string extension = Path.GetExtension(path)

}

Now I need to check if extension is contained in types.
I was looking for a Contain method in Array but I can't find it.

How can I do this?

Thanks,
Miguel
 
S

shapper

Hello,

I am creating a method that given a path and a list of file types
(doc, docx, pdf, ...) checks if the file is of one of those types. I
am using:

public static bool FileType(string path, string[] types) {

  string extension = Path.GetExtension(path)

}

Now I need to check if extension is contained in types.
I was looking for a Contain method in Array but I can't find it.

How can I do this?

Thanks,
Miguel

I think I found a way:

return Array.IndexOf(types, Path.GetExtension(path)) == -1;
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello,

I am creating a method that given a path and a list of file types
(doc, docx, pdf, ...) checks if the file is of one of those types. I
am using:

public static bool FileType(string path, string[] types) {

  string extension = Path.GetExtension(path)

}

Now I need to check if extension is contained in types.
I was looking for a Contain method in Array but I can't find it.

How can I do this?

Thanks,
Miguel

you can use Array.Exist( x => String.Compare( x, path, true)==0);
 
I

Ignacio Machin ( .NET/ C# MVP )

I am creating a method that given a path and a list of file types
(doc, docx, pdf, ...) checks if the file is of one of those types. I
am using:
public static bool FileType(string path, string[] types) {
  string extension = Path.GetExtension(path)

Now I need to check if extension is contained in types.
I was looking for a Contain method in Array but I can't find it.
How can I do this?
Thanks,
Miguel

I think I found a way:

return Array.IndexOf(types, Path.GetExtension(path)) == -1;

You have to check for the extension casing
 
S

shapper

Hello,
I am creating a method that given a path and a list of file types
(doc, docx, pdf, ...) checks if the file is of one of those types. I
am using:
public static bool FileType(string path, string[] types) {
  string extension = Path.GetExtension(path)
}
Now I need to check if extension is contained in types.
I was looking for a Contain method in Array but I can't find it.
How can I do this?
Thanks,
Miguel
I think I found a way:
return Array.IndexOf(types, Path.GetExtension(path)) == -1;

You have to check for the extension casing

I tried your code:

bool a = Array.Exists(types => String.Compare(types, extension, true)
== 0);

But I get an error:
No overload for method 'Exists' takes '1' arguments

What is wrong?

I am not using my method of IndexOf because I need to lower all given
types and extension because Case does not matter in this situation.

Any idea?

Thanks,
Miguel
 
A

Arne Vajhøj

shapper said:
I tried your code:

bool a = Array.Exists(types => String.Compare(types, extension, true) == 0);

But I get an error:
No overload for method 'Exists' takes '1' arguments

What is wrong?

You probbaly want:

bool a = Array.Exists(types, typ => String.Compare(typ, extension, true)
== 0);

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top