简体   繁体   中英

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

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.

Something like this maybe:

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

Obviously, if you have no other information and there are 2 files with the same name and different extensions, you can't do anything (eg 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 (eg somepicture.*) to find the one and only (if you're lucky) file.

在该文件夹中搜索名为somepicture。*的文件,然后上传匹配的文件?

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.

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.

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();
}

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);
        }
    }

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);
}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM