티스토리 뷰
Introduction
There are hundreds and hundreds of classes in the .NET framework and there are some that are really useful which you may never come across. I've been a .NET programmer for several years and I still run across classes in the framework that I've never seen before. I thought it would be cool to document some of the really useful (in my opinion) classes because other programmers may never have known they existed.
System.IO.Path
System.IO.Path
is a static class that provides many useful operations on file or directory path information in a cross-platform manner. Cross-platform manner? Yes, you can run .NET code on non-Windows machines. In case you don't know, check outMono, which is a .NET implementation for Linux, Solaris, Mac and Windows. There are several cool articles right here on Code Project about Mono - just do a search on "mono". If you're writing cross-platform code, being cognisant of your platform is key andSystem.IO.Path
can help.TheSystem.IO.Path
class operates on strings, not on actualFileInfo
,DirectoryInfo
orStream
objects. Most of the methods act on astring
or return astring
. The actual work to create the file needs to be done with extra code.
How do I find the temporary directory?
stringtemppath = Path.GetTempPath(); // will generate something like C:\Documents and Settings\youraccount\Local Settings\Temp
How do I generate a random file name?
stringrandomFile = Path.GetRandomFileName(); // will generate something like utbnd0fm.uyp or ewghyx4x.cvd
How do I generate a random text file?
You can use theChangeExtension()
method in conjunction with theGetRandomFileName()
method.
stringtemptext = Path.ChangeExtension(Path.GetRandomFileName(),".txt"); // generates something like pe054llb.txt
How do I get a unique temp file?
Ever need to generate a uniquely named file for some temporary operation?GetTempFileName()
will actually generate a 0 byte file on the file system and return you the name.
stringtempfile = Path.GetTempFileName(); // will generate something like C:\Documents and Settings\youraccount\Local Settings\Temp\tmp62.tmp
How do I get the file name from a full path?
stringfilepath ="c:\\windows\\somefile.txt" file = Path.GetFileName(filepath);// returns just somefile.txt
How do I get the directory name from a file path?
stringfilepath ="c:\\windows\\somefile.txt" file = Path.GetDirectoryName(filepath);// returns just c:\windows
How do I get the file extension?
stringfilepath ="c:\\windows\\somefile.txt" file = Path.GetExtension(filepath);// returns just .txt
How do I find invalid file name characters?
TheGetInvalidFileNameChars()
will return an array of chars that are invalid to use in a file name. You can use this to check a user supplied file name before attempting to write a file to disk. There is a correspnding method for invalid path chars as well,GetInvalidPathChars()
.
stringuserfile ="junk.t>t"; char[] userchars = userfile.ToCharArray(); char[] invalids = Path.GetInvalidFileNameChars(); foreach(charcinuserchars) { foreach(chariininvalids) { if(c == i) thrownewException("File name contains invalid chars!"); } }
Conclusion
I didn't cover ever last System.IO.Path method or property, but I hit some of the big ones. I'm going to attempt to run these on my Linux box running Mono and update the article.
- Total
- Today
- Yesterday
- KL-2200
- georgia max
- 아이폰 보조배터리
- 릴리스 다이어리 - 설레어
- 아이폰 셀카
- 삼성 외장하드
- 나를 기억하고 있는 너에게
- 보이스티
- God of War III
- KL2200
- 아이튠즈 없이 mp3가져오기
- 아이폰 카메라어플
- Crows Zero
- 모토스톰2
- 릴리스다이어리
- 윈터드림
- 러브트리프로젝트
- 닷넷 엑셀
- 아이팟 보조배터리
- .NET Excel
- IT·컴퓨터
- 로네펠트
- 보이스차
- hot 6
- 닷넷 파일형식
- Roibosh Vanilla
- GTO SHONAN 14DAYS
- 안녕 바다
- Lily's Diary
- 켄우드 정수기
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |