cduo
cduo

Reputation: 33

File extension - c#

I have a directory that contains jpg,tif,pdf,doc and xls. The client DB conly contains the file names without extension. My app has to pick up the file and upload the file. One of the properties of the upload object is the file extension.

Is there a way of getting file extension if all i have is the path and name

eg:

C:\temp\somepicture.jpg is the file and the information i have through db is

c:\temp\somepicture

Upvotes: 3

Views: 2393

Answers (11)

James Manning
James Manning

Reputation: 13579

All the pieces are here in the existing answers, but just trying to unify them into one answer for you - given the "guaranteed unique" declaration you're working with, you can toss in a FirstOrDefault since you don't need to worry about choosing among multiple potential matches.

static void Main(string[] args)
{
    var match = FindMatch(args[0]);
    Console.WriteLine("Best match for {0} is {1}", args[0], match ?? "[None found]");
}

private static string FindMatch(string pathAndFilename)
{
    return FindMatch(Path.GetDirectoryName(pathAndFilename), Path.GetFileNameWithoutExtension(pathAndFilename));
}

private static string FindMatch(string path, string filename)
{
    return Directory.GetFiles(path, filename + ".*").FirstOrDefault();
}

Output:

> ConsoleApplication10 c:\temp\bogus
Best match for c:\temp\bogus is [None found]
> ConsoleApplication10 c:\temp\7z465
Best match for c:\temp\7z465 is c:\temp\7z465.msi
> ConsoleApplication10 c:\temp\boot
Best match for c:\temp\boot is c:\temp\boot.wim

Upvotes: 0

Guffa
Guffa

Reputation: 700252

From the crippled file path you can get the directory path and the file name:

string path = Path.GetDirectoryName(filename);
string name = Path.GetFileName(filename);

Then you can get all files that matches the file name with any extension:

FileInfo[] found = new DirectoryInfo(path).GetFiles(name + ".*");

If the array contains one item, you have your match. If there is more than one item, you have to decide which one to use, or what to do with them.

Upvotes: 0

João Angelo
João Angelo

Reputation: 57658

One more, with the assumption of no files with same name but different extension.

string[] files = Directory.GetFiles(@"c:\temp", @"testasdadsadsas.*");

if (files.Length >= 1)
{
    string fullFilenameAndPath = files[0];

    Console.WriteLine(fullFilenameAndPath);
}

Upvotes: 0

David Larrabee
David Larrabee

Reputation: 384

you could do something like this perhaps....

    DirectoryInfo di = new DirectoryInfo("c:/temp/");
    FileInfo[] rgFiles = di.GetFiles("somepicture.*");
    foreach (FileInfo fi in rgFiles)
    {
        if(fi.Name.Contains("."))
        {
            string name = fi.Name.Split('.')[0].ToString();
            string ext =  fi.Name.Split('.')[1].ToString();

            System.Console.WriteLine("Extension is: " + ext);
        }
    }

Upvotes: 0

Adam Robinson
Adam Robinson

Reputation: 185623

You could obtain a list of all of the files with that name, regardless of extension:

public string[] GetFileExtensions(string path)
{
    System.IO.DirectoryInfo directory = 
        new System.IO.DirectoryInfo(System.IO.Path.GetDirectoryName(path));

    return directory.GetFiles(
        System.IO.Path.GetFileNameWithoutExtension(path) + ".*")
        .Select(f => f.Extension).ToArray();
}

Upvotes: 0

Josh Warner-Burke
Josh Warner-Burke

Reputation: 87

Something like this maybe:

DirectoryInfo D = new DirectoryInfo(path);
foreach (FileInfo fi in D.GetFiles())
{
    if (Path.GetFileNameWithoutExtension(fi.FullName) == whatever)
        // do something
}

Upvotes: 3

Jonathan S.
Jonathan S.

Reputation: 541

You would have to use System.IO.Directory.GetFiles() and iterate through all the filenames. You will run into issues when you have a collision like somefile.jpg and somefile.tif.

Sounds like you have bigger issues than just this and you may want to make an argument to store the file extension in your database as well to remove the ambiguity.

Upvotes: 0

Samuel Neff
Samuel Neff

Reputation: 74909

Use Directory.GetFiles(fileName + ".*"). If it returns just one file, then you find the file you need. If it returns more than one, you have to choose which to upload.

Upvotes: 5

Bryan Rowe
Bryan Rowe

Reputation: 9303

Get the lowest level folder for each path. For your example, you would have: 'c:\temp\'

Then find any files that start with your filename in that folder, in this case: 'somepicture'

Finally, grab the extension off the matching filename. If you have duplicates, you would have to handle that in a unique way.

Upvotes: 0

driis
driis

Reputation: 164291

Search for files named somepicture.* in that folder, and upload any that matches ?

Upvotes: 0

Tomas Vana
Tomas Vana

Reputation: 18775

Obviously, if you have no other information and there are 2 files with the same name and different extensions, you can't do anything (e.g. there is somepicture.jpg and somepicture.png at the same time).

On the other hand, usually that won't be the case so you can simply use a search pattern (e.g. somepicture.*) to find the one and only (if you're lucky) file.

Upvotes: 0

Related Questions