tag:blogger.com,1999:blog-6497434045878013201.post3672894899653145699..comments2023-12-06T18:45:51.800-08:00Comments on Dot Net Tips & Tricks , C# (C Sharp)Tips & Tricks: Check if Path is Absolute or Relative Path using C# (.NET)Unknownnoreply@blogger.comBlogger8125tag:blogger.com,1999:blog-6497434045878013201.post-56466914305374019842014-11-19T03:18:19.511-08:002014-11-19T03:18:19.511-08:00Thanks @eldor!
Little fix:
var ex = new Regex(@&q...Thanks @eldor! <br />Little fix:<br />var ex = new Regex(@"^(([a-zA-Z]:\\)|(//)).*");<br /><br />cos of UNC path started with // like '//myserver/my_share/'Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6497434045878013201.post-27291180257095336092014-02-28T08:22:04.000-08:002014-02-28T08:22:04.000-08:00Unfortunately IsPathRooted does not mean that it i...Unfortunately IsPathRooted does not mean that it is an absolute path(=completely deterministic path, which points to the same place from everywhere). It returns True for "c:file.txt", "\file.txt" as well. <br /><br />The matching regexp can be something similar:<br />var ex = new Regex(@"^(([a-zA-Z]:\\)|(\\\\)).*");<br /><br />eldorhttps://www.blogger.com/profile/16548990510542719043noreply@blogger.comtag:blogger.com,1999:blog-6497434045878013201.post-11794365555058700212012-09-06T12:50:44.360-07:002012-09-06T12:50:44.360-07:00Starting a path with "\" means "sta...Starting a path with "\" means "start at the root directory" effectively making the path a rooted path. Using ".\" and "..\" mean "current directory" and "current directory parent" making them relative paths.<br /><br />Try and see:<br />Console.WriteLine(Path.IsPathRooted(@"\"));<br />Console.WriteLine(Path.IsPathRooted(@".\"));<br />Console.WriteLine(Path.IsPathRooted(@"..\"));Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6497434045878013201.post-13502620496929243772012-08-27T17:47:49.188-07:002012-08-27T17:47:49.188-07:00probably because you have \temp\ in your root fold...probably because you have \temp\ in your root folder.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6497434045878013201.post-36241721117573163212012-03-08T10:32:37.365-08:002012-03-08T10:32:37.365-08:00Even if the post is older, if this hint can help s...Even if the post is older, if this hint can help somebody then :<br /><br />if (System.IO.Path.IsPathRooted(path)) {<br /> Console.WriteLine("Path is absolute");<br />} else {<br /> Console.WriteLine("Path is relative");<br />}Anthonyhttps://www.blogger.com/profile/18080266687570232269noreply@blogger.comtag:blogger.com,1999:blog-6497434045878013201.post-77664949976989704462012-03-08T10:31:15.211-08:002012-03-08T10:31:15.211-08:00Even if the post is older, if this hint can help s...Even if the post is older, if this hint can help somebody then :<br />Use instead System.IO.Path.IsPathRooted(path)Anthonyhttps://www.blogger.com/profile/18080266687570232269noreply@blogger.comtag:blogger.com,1999:blog-6497434045878013201.post-12608955698382964122009-03-06T00:36:00.000-08:002009-03-06T00:36:00.000-08:00I think there it is better to use Regular Expressi...I think there it is better to use Regular Expressions:<BR/><BR/> Regex meinReg = new Regex("^[a-zA-Z]{1}:\\.*");<BR/> if (meinReg.Match(path) == Match.Empty)<BR/> {<BR/> return true;<BR/> }<BR/> return false;<BR/><BR/>Best regards ;-)Philhttps://www.blogger.com/profile/05570530558531844158noreply@blogger.comtag:blogger.com,1999:blog-6497434045878013201.post-26722528346420595072009-02-05T02:15:00.000-08:002009-02-05T02:15:00.000-08:00but it returns true even if you give \temp\but it returns true even if you give \temp\Anonymousnoreply@blogger.com