Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
i try to get time of file. scanerio is when any last file is saved in folder i want that file date time in console
i try this

What I have tried:

C#
public static void Main()
        {
            try 
            {
                string path = @"C:\Users\Administrator\Desktop\New folder\DB.txt";
                if(!File.Exists(path))
                {
                    File.Create(path);
                }
                else
                {
                    File.SetLastWriteTime(path, new DateTime());
                }
                DateTime dt = File.GetLastWriteTime(path);
                Console.WriteLine("the last time {0}", dt);
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }

        }


but this shows error
Not a valid Win32 FileTime.
any solution
Posted
Updated 9-May-16 23:32pm
Comments
Sinisa Hajnal 10-May-16 5:29am    
Use Now() function instead of new DateTime...if that doesn't work, check the documentation on Win32 file time and see which formats are supported.
Suvendu Shekhar Giri 10-May-16 5:33am    
Now is a property of DateTime class, it's not a function or method.
Ref: DateTime.Now Property (System)[^]
Sergey Alexandrovich Kryukov 10-May-16 9:14am    
In what line?

There are no situations when hard-code path name can be useful. In your case, the path you are using may not exist, or not accessible to your application. The whole idea is wrong. What are you trying to achieve?

Now, look what you are doing. Do you really want to set file time and then immediately read it. Why?

—SA
Philippe Mori 10-May-16 12:30pm    
The system automatically handle file creation and last modification date.

Thus in most cases, it make no sense to change either time.

1 solution

What you want to achieve through following line of code?
C#
File.SetLastWriteTime(path, new DateTime());

If you want to set current datetime as LastWriteTime, try following-
C#
File.SetLastWriteTime(path, DateTime.Now);


If you still experience the same error or if your requirement is something else, please let me know.
:)
 
Share this answer
 
Comments
super_user 10-May-16 5:41am    
i dont want current date time .. i want the time when file is created / modified in folder
Suvendu Shekhar Giri 10-May-16 5:45am    
..and that didn't answer my question, what is the use of following line-
File.SetLastWriteTime(path, new DateTime());
Please explain.
super_user 10-May-16 5:47am    
through this line i try to show date time when the file is created/modified
Suvendu Shekhar Giri 10-May-16 5:57am    
Then I would suggest to try commenting following lines of code and see if that helps-
//else
// {
// File.SetLastWriteTime(path, new DateTime());
// }
super_user 10-May-16 6:22am    
ok if i create any new file in new folder .and i want that file date time then what i can do .. i also remove db.txt from path then when i create new file in folder and then i run to display date time then this shows error Access to the path 'C:\Users\Administrator\Desktop\New folder' is denied.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900