Prev: null reference exception could result at runtime
Next: Use a static form of the EventLog class?
From: Armin Zingler on 7 Apr 2010 18:04 Am 07.04.2010 23:16, schrieb Saga: > >> I'd do it your way - and switch Option Strict On. >> > Thanks for the reply. Option strict ON? I have been lax with this. I > can only wonder what changes I'll have to do to the code, luckily I > just started o this project in Feb 2010, so if I am going to do this > it is best to do it now before I accumulate too many lines of code. You can try it on a per file basis before enabling it for the whole project. Implicit conversions (with Option Strict OFF) can be welcome or not, and some of them will even never succeed at runtime. As avoiding mistakes should have a higher priority than typing fewer letters, those unwelcome implicit conversions or those done in an unintended way must be excluded with every assembly created. For this, switching Option Strict On is the only guarantee. It is irresponsible not to make use of it. In other words, you should be aware of the data (and data types) your shifting around, and if there is something to convert, convert explicitly and deliberately. One day I'll put this in my sig. ;-) -- Armin
From: Phill W. on 8 Apr 2010 08:36 On 07/04/2010 20:43, Saga wrote: > For Each objFSI As System.IO.FileSystemInfo In > objDI.GetFileSystemInfos() > > If objFSI.Attributes() And IO.FileAttributes.Directory Then > strType = "Folder" > Else > strType = "File" > End If > Next If that's /all/ you're doing with these items, then this method will serve as well as any other. If you need to do significantly /different/ things with Files and Folders (for example, you might want to recursively scan through directories) then you might want to process them separately: Using System.IO For Each eDir as String in Directory.GetDirectories(strPath, "*.*") ' eDir holds the full path to the directory . . . Next For Each eFile as String in Directory.GetFiles( strPath, "*.*") ' eFile holds the full path to the file . . . Next HTH, Phill W.
From: Saga on 8 Apr 2010 11:07 "Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-k> wrote in message news:hpkik6$56i$1(a)south.jnrs.ja.net... > On 07/04/2010 20:43, Saga wrote: > >> For Each objFSI As System.IO.FileSystemInfo In >> objDI.GetFileSystemInfos() >> >> If objFSI.Attributes() And IO.FileAttributes.Directory Then >> strType = "Folder" >> Else >> strType = "File" >> End If >> Next > > If that's /all/ you're doing with these items, then this method will serve > as well as any other. > I am examining each file's creation date and if older than X days I delete the file. The above was the code that I had at the time I posted my question. The rotuine that I have now does use recursion. Thank you for your reply. Regards, Saga > If you need to do significantly /different/ things with Files and Folders > (for example, you might want to recursively scan through directories) then > you might want to process them separately: > > Using System.IO > > For Each eDir as String in Directory.GetDirectories(strPath, "*.*") > ' eDir holds the full path to the directory > . . . > Next > > For Each eFile as String in Directory.GetFiles( strPath, "*.*") > ' eFile holds the full path to the file > . . . > Next > > HTH, > Phill W.
From: Patrice on 8 Apr 2010 11:21 IMO it depends what you'll actually do within the loop. My personal preference is to use two loops, one using GetDirectories, one using GetFiles but this is because most of the time I'm doing something different (such as processing files and doing a recursive call for directories). -- Patrice "Saga" <antiSpam(a)nowhere.com> a �crit dans le message de groupe de discussion : esWBTqo1KHA.5328(a)TK2MSFTNGP04.phx.gbl... > Hi all. Now I am processing data in folders. I get a listing of folder > contents and have to test to determine whether item is folder or > file. I have this routine: > > strPath is previously set with valid data, strType is previously defined. > > Dim objDI As New System.IO.DirectoryInfo(strPath) > > For Each objFSI As System.IO.FileSystemInfo In > objDI.GetFileSystemInfos() > > If objFSI.Attributes() And IO.FileAttributes.Directory Then > strType = "Folder" > Else > strType = "File" > End If > > Next > > My question is whether this is the best way to do this. I "explored" a bit > and > did not find any other obvious way. Any feedback is welcomed! Thanks, Saga > > >
First
|
Prev
|
Pages: 1 2 Prev: null reference exception could result at runtime Next: Use a static form of the EventLog class? |