Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# Windows C#4.0 , +
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:
 
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 5-Jul-12 9:48am
Comments
Sergey Alexandrovich Kryukov at 5-Jul-12 15:35pm
   
Why?! You are doing really bad thing, no wonder you have problems -- please see my answer.
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Wes Aday at 5-Jul-12 16:24pm
   
Not to mention posting accurate code. A console application without a main method. 5
Sergey Alexandrovich Kryukov at 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
Member 7749624 at 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)

  Print Answers RSS
0 OriginalGriff 400
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 264
3 BillWoodruff 245
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 5 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100