第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

在忽略路徑和文件名中的大小寫的同時(shí)打開文件

在忽略路徑和文件名中的大小寫的同時(shí)打開文件

C#
德瑪西亞99 2022-11-22 16:00:15
我可以轉(zhuǎn)換pathAndFilename為小寫,但我需要一種方法來區(qū)分OpenRead大小寫。// pathAndFileName has been converted with .ToLower()using (FileStream fileStream = File.OpenRead(pathAndFileName)){    Bitmap bitmap = new Bitmap(fileStream);    Image image = (Image)bitmap;}
查看完整描述

3 回答

?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

如果您嘗試訪問運(yùn)行 Linux 或文件名區(qū)分大小寫的其他操作系統(tǒng)的計(jì)算機(jī)上的文件,解決方法可能是(未測試?。┦褂媚鷵碛械奈募鳛槟J絹砹谐瞿夸浿械奈募?。請注意,可能有多個(gè)文件具有相同的名稱,只是不同的拼寫形式。在這種情況下,此輔助函數(shù)將引發(fā)異常。


static void Main(string[] args)

{

    string pathAndFileName = ..your file name...;

    string resultFileName = GetActualCaseForFileName(pathAndFileName);


    using (FileStream fileStream = File.OpenRead(resultFileName))

    {

        Bitmap bitmap = new Bitmap(fileStream);

        Image image = (Image)bitmap;

    }    



    Console.WriteLine(resultFileName);

}


private static string GetActualCaseForFileName(string pathAndFileName)

{

    string directory = Path.GetDirectoryName(pathAndFileName);

    string pattern = Path.GetFileName(pathAndFileName);

    string resultFileName;


    // Enumerate all files in the directory, using the file name as a pattern

    // This will list all case variants of the filename even on file systems that

    // are case sensitive

    IEnumerable<string> foundFiles = Directory.EnumerateFiles(directory, pattern);


    if (foundFiles.Any())

    {

        if (foundFiles.Count() > 1)

        {

            // More than two files with the same name but different case spelling found

            throw new Exception("Ambiguous File reference for " + pathAndFileName);

        }

        else

        {

            resultFileName = foundFiles.First();

        }

    }

    else

    {

        throw new FileNotFoundException("File not found" + pathAndFileName, pathAndFileName);

    }


    return resultFileName;

}


查看完整回答
反對 回復(fù) 2022-11-22
?
蕪湖不蕪

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊

我受到包含 GetActualCaseForFileName() 的答案的啟發(fā),但它不適用于我的 Xamarin iOS 項(xiàng)目。


我創(chuàng)建了以下代碼,這似乎對我有用:


public string getCaseNudgedPathName( string origPath) {

    var retPath = origPath;

    var dir = Path.GetDirectoryName( origPath);

    var pattern = Path.GetFileName( origPath);


    var foundFiles = Directory.GetFiles(dir);

    int countMatch = 0;

    foreach (var foundFile in foundFiles) {

        if ( foundFile.Equals(origPath,IgnoreCase)) {

            countMatch++;

            retPath = foundFile;

        }

    }

    if (countMatch > 1) { 

        throw new Exception( $"Ambiguous filename '{pattern}'");

    } 

    return retPath;

}


查看完整回答
反對 回復(fù) 2022-11-22
?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊

對于區(qū)分大小寫的文件和目錄,您需要結(jié)合第一個(gè)答案和:


private static string GetActualCaseForFileName(string pathAndFileName)

{

    string directory = Path.GetDirectoryName(pathAndFileName);

    string directoryCaseSensitive = GetDirectoryCaseSensitive(directory)

    ...

}



private static string GetDirectoryCaseSensitive(string directory)

{

  var directoryInfo = new DirectoryInfo(directory);

  if (directoryInfo.Exists)

  {

    return directory;

  }


  if (directoryInfo.Parent == null)

  {

    return null;

  }


  var parent = GetDirectoryCaseSensitive(directoryInfo.Parent.FullName);

  if (parent == null)

  {

    return null;

  }


  return new DirectoryInfo(parent).GetDirectories(directoryInfo.Name, new EnumerationOptions {MatchCasing = MatchCasing.CaseInsensitive}).FirstOrDefault()?.FullName;

}


查看完整回答
反對 回復(fù) 2022-11-22
  • 3 回答
  • 0 關(guān)注
  • 149 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號