C# : Path

[".0s4*0|collections;datetime-format;path",["F@eEMX","JCBEJABEBGKAJC","OBOYGDGDHDFOCOBBSTUUUUTTUUUUPFOCOBPADAWBCSTTUUUUTTUUUU","swst.",".NET","Array","Dictionary","List","String","2D","Async","Console","DataTable","Dates","DateTime","Enum","File","For","Foreach","Format","IEnumerable","If","IndexOf","Lambda","LINQ","Optimization","Parse","Path","Process","Property","Random","Regex","Replace","Sort","Split","Static","Substring","Switch","Tuple","While","Path.GetDirectoryName"," finds a directory name from a path. We look at this .NET Framework method. We then look inside it and develop an optimized version. It is possible to remove certain features from its implementation. ","Directory ","directory","Example."," First, there are many advantages to the Path static class in the base class library, but it also has drawbacks. It checks extensively for errors. How much you want error detection and correction depends on your application. ","C# program that uses Path.GetDirectoryName","\n\nusing System;\nusing System.IO;\n\nclass Program\n{\n static void Main()\n {","\n // Example path.\n ","string path = @\"C:\\Users\\Sam\\Documents\\Test.txt\";","\n\n // Get the directory name.\n ","Console.WriteLine(","Path.GetDirectoryName","(path));\n }\n}\n\n","Output","\n\nC:\\Users\\Sam\\Documents","The Path static class"," tries to detect all invalid paths and fix them if necessary. However, these automatic fixes can cause more harm than good. Internally, it runs large and complicated internal routines. ","Note: ","Many developers advise using the Path static class methods such as GetDirectory.","Example 2."," Next, we look at an alternate way you can get the directory part of the string. You can simplify GetDirectory by eliminating the first steps in the algorithm noted above, and simply using LastIndexOf and Substring. ","LastIndexOf ","lastindexof","Substring ","substring","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","C# program that uses alternative path method","\n\nusing System;\nusing System.IO;\n\nclass Program\n{\n static void Main()\n {\n string p = @\"C:\\Users\\Sam\\Documents\\Test.txt\";\n Console.WriteLine(GetDirectoryName(p));\n }\n\n static string GetDirectoryName(string f)\n {\n try\n {\n return f.Substring(0, f.LastIndexOf('\\\\'));\n }\n catch\n {\n return string.Empty;\n }\n }\n}\n\n","Output","\n\nC:\\Users\\Sam\\Documents","What are the advantages"," with the custom method shown? It is more predictable and traps its own exceptions, making for potentially simpler calling code. It is also ten times faster. ","Note: ","This makes sense because it has two lines of code compared to hundreds or more in Path.GetDirectory.","Performance."," You may be wondering how must faster the custom method shown is than the static Path method. I was interested in speed here because he runs this code almost 100,000 times a day. The computer running it is also slow. ","Benchmark ","benchmark","Benchmark notes","\n\nIterations: 12000000\nInput string: @\"C:\\Users\\Sam\\Documents\\Test.txt\n\nPath.GetDirectoryName time: ","6780 ms","\n String method: "," 547 ms","Summary."," We looked into Path.GetDirectoryName with IL Disassembler. We observed what it does internally. Then we took the very core of the code and extracted it into an alternative method. We tested the results. ","IL Disassembler ","il-disassembler","Tip: ","To simplify your program's control flow, you can copy and paste the internal implementations of the .NET Framework methods.","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"],"url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAL4AAABsCAMAAADjXPiLAAAAMFBMVEX///9522Pv/un1//JS0zDB77bT9Myw6aWU4oXp/+H5//ft/+b8//ry/+3h+Ns1zgBIqVA6AAAC7UlEQVR4Xu3Y65KqMBAEYHIP9/d/23NWZAiNVFxr1aZq+p8KVR84w4Q088P4HNp9xq5hzHwWE+EC7EDNx/jUQhw3H8PuRz4moL+7FN8jv+fm18uHlb8C2pC98M0PoBvh9lPz2zYa8dsbYOjFP/Dz2yj8tAAGqB5ufiu3P98BVqbvFfhhK/4lncxeen5ZPb6B6umvwG+PX69R/stR/jWfPJdq3YR8d6kHp4Gp29grTd0g30aonbbj50cvfIdLth9Awkxun+GL/Clt+iyFX5T+VH0tcJ/nmzWlK9j/wbfFNGMy8EeOty0joPK5k6svlT0F30fk34raz4dEPI53o8Q9sSXR8W5TRUFXip9rk7C/V0R6oknsl/kmIUjGrVk7o+ziH3M5nL/H9yanWMp7O7rheIYp/4mw3HH36eL/9cp4KpdDsehyVy4uHCs/lcshVxR/V55tWflS7m7XCPfjZXHEyvcreH8xcZm0owwuTr5dvXlfSsnuV3eOkx/Fu2/k7Pb7QSMbHzsXHqPd/vSek2/gePwsg4uOj0MLerfZ927HyI+w9dylrRegdxn5og13bYS/YygH1wyprvTexsdamdZSlzmA5zPy/QoQrMELksH1Cv+9scdS2coJi5+PH46NGrCZZXDx8dPxMTmJBaqv5+NLodtGFpjSDk6+W0LHd/ODx4yBwWW3wWWKbOYt+WbeMr6XHx/V+ZODS/iV+fKJ0veP7quxt0jxWDa+SB+nhbDxPXhrW4Vc/Am41X1yLn4AbnWrkIufgVvdJ+fim7mSCfxUfIfa+j45Ex/eTGRxdj64Rib+qfL0uiwT/3xxU6Fx8AXgEIAX1suF8fCns5fy/lBWowwuHr4Y86FBw9lP9lf8uOYO6Nac/vDK0AqHW2xlcOEf8wy/fshfFJ6X4QQFPhS/WbANBPw/iPKVr3zlK1/5yle+8pWvfOVXIm4GvvKJonzlK99AhG+XMPFpx5bylf96lK985Stf+cpXvvKVr/x/2oEPrjSYUD0AAAAASUVORK5CYII=)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACCAgMAAADasxWRAAAADFBMVEX/6Nn///+VlZX29va5ksB5AAAASElEQVR4AWPACZj/f0DmjkqOSjKtQgcLyJccZiEE9SOmP8mXHEaBMBoIo4EwGggryJccvvVKKAogTnJUclRyVHLklgmjkqOSAALWyrsxEHLDAAAAAElFTkSuQmCC)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAL4AAABBCAMAAABYbu5jAAAAMFBMVEX///+1wv+jqv9siv/p6//W3f8zZv9gb//19v9NK/8zAP999//S//8z//8zzP+z6P/0gInFAAABg0lEQVR4XtXXy1ICURAD0H7c96D+/9/KQqkhlEKD0iTrWZzKYtJXbou2s+jvn1Q55f2mfAMOb6Ec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