473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

problems with C# equivalent in VB.NET Folder & File Class

Hi experts, I have a problem when converting vb codes to c# codes. What library should I import? Anyone can tell me how to convert below codes into c# format?

Thank in advance.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Filechecking(ByRef fso As Scripting.FileSystemObject, ByVal iDataKeepDays As Integer, ByVal tmpPath As String)
  2.         Dim Folder As Folder
  3.         Set Folder = fso.GetFolder(tmpPath)
  4.         Dim f As File
  5.  
  6.         For Each f In Folder.Files
  7.  
  8.             If DateDiff("D", f.DateLastModified, Now) > iDataKeepDays Then
  9.                 DoEvents
  10.                fso.DeleteFile f.Path
  11.            End If
  12.        Next
  13.  
  14.        Set f = Nothing
  15.        Set Folder = Nothing
  16.  
  17. End Sub
  18.  
Jan 31 '18 #1
1 2156
Luuk
1,047 Expert 1GB
Because i'm learning c# meself, I tried to write it for you.
Well, not completely, but to get you started... ;)

Expand|Select|Wrap|Line Numbers
  1.         static void Main(string[] args)
  2.         {
  3.             string TempPath = @"c:\temp\";
  4.             int iDataKeepDays = 14;
  5.             RemoveOlder(TempPath, iDataKeepDays);
  6.  
  7.             Console.ReadLine();
  8.         }
  9.  
  10.         static void RemoveOlder(string TempPath, int days)
  11.         {
  12.             foreach (string file in Directory.GetFiles(TempPath))
  13.             {
  14.                 DateTime lastWriteTime = File.GetLastWriteTime(file);
  15.                 int age = (DateTime.Now - lastWriteTime).Days;
  16.                 Console.WriteLine("File: {0} is {1} days old", file, age);
  17.             }
  18.         }
  19.  
A quicker example (on my test this works almost twice as fast
Expand|Select|Wrap|Line Numbers
  1.    static void RemoveOlder2(string TempPath, int days)
  2.         {
  3.             string[] files = Directory.GetFiles(TempPath);
  4.             var result = files.Where(file => (DateTime.Now - File.GetLastWriteTime(file)).Days > days);
  5.             foreach (var file in result)
  6.             {
  7.                 DateTime lastWriteTime = File.GetLastWriteTime(file);
  8.                 int age = (DateTime.Now - lastWriteTime).Days;
  9.                 Console.WriteLine("File: {0} is {1} days old", file, age);
  10.             }
  11.         }
  12.  
Feb 3 '18 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Marcus | last post by:
I am trying to auto-detect what folder a file is sitting in and based upon this info, query a MySQL database to get further information. I have been all over php.net and been through numerous...
2
by: Pierre Rouleau | last post by:
Greetings, I'm wondering why the >> operator does not use the write() method of a class derived from the built-in file class as in DerivedFile below. In the following example: - StringFile...
0
by: arshad | last post by:
Hi, I am facing some problems in reading XML file into dataset in vb.net. The XML is having some duplicate tabels. I am getting the error message saying "The same table(imei_details) cannot be...
3
by: Mark | last post by:
Hello, How do you manipulate folders and files in vb.net. I know in vb6 you used filesystemobject, but how do you do it in vb.net? Any help would be greatly appreciated! Thanks in advance.
0
by: drawing in aspnet | last post by:
Question about putting the data layer in a separate class library. I keep reading that the data layer should be separated from the presentation layer and put in its own class library. I am...
0
by: mrinalaryan | last post by:
create folder & upload file in the web server through web service-C# -------------------------------------------------------------------------------- im using web service to connect windows form...
6
monirul arfin
by: monirul arfin | last post by:
Hi all, I want to secure my folder or file, so no one can open it or see it. How can I do it , is there any option in windows , without creating a new account in computer. monirul
3
by: Vishy147 | last post by:
Hi, I am new to python. I have to devlope a small for code for getting the path of a particular folder, that is given as input to the code, in the other folder. Basically i have to search a folder,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.