Click here to Skip to main content
11,795,848 members (79,104 online)
Rate this: bad
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++)
        public void getfol(string path)
            int i;
                string[] a = Directory.GetDirectories(path);
                for ( i = 0; ; i++)
                            bool isa = ((File.GetAttributes(a[i]) & FileAttributes.System) == FileAttributes.System);
                            if (isa == true)
                        if (Directory.GetDirectories(a[i]).Length > 0)

                        if (Directory.GetFiles(a[i]).Length > 0)
            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,
Posted 5-Jul-12 8:48am
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.

1 solution

Rate this: bad
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.

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.
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.

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

  Print Answers RSS
0 Maciej Los 410
1 Abhinav S 368
2 OriginalGriff 300
3 Sergey Alexandrovich Kryukov 266
4 KrunalRohit 249
0 OriginalGriff 1,852
1 Maciej Los 1,465
2 KrunalRohit 1,300
3 CPallini 840
4 Richard MacCutchan 828

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 5 Jul 2012
Copyright © CodeProject, 1999-2015
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