Click here to Skip to main content
15,891,427 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi guys,
I have written a program to retrieve the contents of a pc. But this program worked only in windows xp.I was unable to perform its full functions in windows 7.


Code is here:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security;
using System.Security.Permissions;

namespace ConsoleApplication2
{
    class Class1
    {
        StreamWriter sw = new StreamWriter("D:\\contents.txt");
        public void getfil(String path)
        {
            string[] p = Directory.GetFiles(path);
            for (int j = 0; j < p.Length; j++)
            {
                sw.WriteLine(p[j]);
                Console.WriteLine(p[j]);
            }
        }
        public void getfol(string path)
        {
            
            int i;
            try
            {
                string[] a = Directory.GetDirectories(path);
                for ( i = 0; ; i++)
                {
                
                    sw.WriteLine(a[i]);
                    Console.WriteLine(a[i]);
                   
                            bool isa = ((File.GetAttributes(a[i]) & FileAttributes.System) == FileAttributes.System);
                            if (isa == true)
                            {
                                sw.WriteLine("Idetifier");
                                Console.WriteLine("");

                                i++;

                            }
                           
                        if (Directory.GetDirectories(a[i]).Length > 0)
                        {
                            
                                getfol(a[i]);
                            

                        }


                        if (Directory.GetFiles(a[i]).Length > 0)
                        {
                                getfil(a[i]);
                            
                
                       }
                        
                }
                sw.Close();
                Console.WriteLine("Done");
            }
            
            catch (Exception e)
            
            {
              
            }
          
        }
    }
}


In the given code i am able to retrieve the c:\\windows contents in windows xp but when i do same thing in windows 7 it dint works. Can anyone please help me out in this scenario.


Suggestion Required,
Regards,
Akky
Posted
Comments
Sergey Alexandrovich Kryukov 5-Jul-12 15:35pm    
Why?! You are doing really bad thing, no wonder you have problems -- please see my answer.
--SA

1 solution

This code makes no sense even in XP, it's working only by luck. I don't want to analyze it all. Look at "D:\\contents.txt". This file location is illegal in Windows 7 and makes no sense event in XP. Of course, you can do some administration work on each system and make "D:" accessible, but it makes no sense. How do you know that "D:" even exists on a target machine? Even "C:" does not have to be available (on one of my machines disk "C:" does not exists, the system drive is "E", it happens so as a result of system upgrade). There are no situations where a hard-coded path names can be useful. All the path names should be calculated during run time from various sources of information: calculated location of the application entry assembly, location of special directories, per user or for "All Users", configuration files or other user data.

Besides, using any immediate constants in the code is no good, bad for maintenance. You should either use explicit constants, preferably gathered in special files and static classes, or resources, or data files.

Just write your code accurately; and it will work on all platforms.

Besides, use exception handling properly. You can catch all exceptions on the very top of the stack of each thread. Also, all exceptions should be caught in the main UI event-handling cycle; each UI library has the mechanism to catch them. What you do is like committing the crime against yourself: you are catching exception is some function and block its propagation. Never do it (rare exclusions apply). You just make exception silent, effectively blocking powerful exception mechanism. You should not use exception block in this function at all. If you did not commit this crime against yourself, you would know your problem in all detail already. And also, use the debugger.

—SA
 
Share this answer
 
Comments
[no name] 5-Jul-12 16:24pm    
Not to mention posting accurate code. A console application without a main method. 5
Sergey Alexandrovich Kryukov 5-Jul-12 16:29pm    
Well, it could be in another file... not the root of the problem, but yes, as a question post it's inaccurate (do you know many who did better at this forum though? :-)
Thank you, Wes.
--SA
Akkywadhwa 6-Jul-12 4:01am    
Ok....Sergey if u consider this code as incorrect then will you please a code snippet for me to retrieve the contents of the pc and saving it on desktop for both the windows xp and 7.
Akkywadhwa

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