The following code snippet returns the extension of a file. Extension; Console. Don't forget to import System. IO and System. Text namespaces in your project.
The following code example uses a FileInfo class to create an object by passing a complete filename. The FileInfo class provides properties to get information about a file such as file name, size, full path, extension, directory name, is read only, when the file was created and last updated. Name; Console. FullName; Console. DirectoryName; Console. Exists; Console. I only needed images, but it was possible that the web call could send back something else like a web page or different file type all together.
I only wanted to save the file if it was an image type and if the image type was one of the common ones that I expected. The answer that made the most sense to me was pointed out that a lot of image types have specific sets of bytes at the beginning of the file type to denote which type of file it is.
The post referenced pretty much all of the image file types that I wanted to use. Anything jpeg, png, gif, or bmp was exactly what I was expecting to see. This allowed me to pass in a stream from the url and compare it to any of these byte arrays and detect the file type.
This also enabled me to download and save the files locally with the correct extension. Without that, everybody is just guessing what exactly you did The third most sensible thing but the thing I'd have done first would be to use the debugger to look at exactly what is going on when I ran the code. Think about it: when you change a file name it makes no change at all to the file content - and an array of bytes is just the file content without any other "wrapping" material that you might get with a filestream.
So at a guess, you are trying to cast a byte array to a stream, and assuming that that will give you a file name. It won't. Posted May pm OriginalGriff. Add your solution here. OK Paste as. Treat my content as plain text, not as HTML. Existing Members Sign in to your account. This email is in use. Do you need your password? Submit your solution! When answering a question please: Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad spelling and grammar. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome. Don't tell someone to read the manual. Chances are they have and don't get it.
0コメント