在Windows 7中的vs2008中检索C#中的PC内容时出错 [英] error in retrieving pc's content in c# in vs2008 in windows 7

查看:80
本文介绍了在Windows 7中的vs2008中检索C#中的PC内容时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我已经编写了一个程序来检索PC的内容.但是该程序只能在Windows XP中工作,我无法在Windows 7中执行其全部功能.


代码在这里:

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)
            
            {
              
            }
          
        }
    }
}



在给定的代码中,我能够在Windows XP中检索c:\\ windows内容,但是当我在Windows 7中执行相同的操作时,它的工作就可以了.在这种情况下,谁能帮助我.


需要建议,
问候,
Akky



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

推荐答案

即使在XP中,该代码也没有意义,仅靠运气.我不想分析所有这一切.查看"D:\\ contents.txt".该文件位置在Windows 7中是非法的,在XP中没有任何意义的事件.当然,您可以在每个系统上进行一些管理工作,并使"D:"可访问,但这没有任何意义.您怎么知道目标机器上甚至存在"D:"?甚至"C:"也不必可用(在我的一台计算机上,磁盘"C:"不存在,系统驱动器为"E",这是系统升级的结果). 在任何情况下,硬编码的路径名都不是有用的.所有路径名都应在运行时从各种信息源中计算得出:应用程序输入程序集的计算位置,特殊目录的位置,每个用户或所有用户",配置文件或其他用户数据.

此外,在代码中使用任何立即常量都无济于事,不利于维护.您应该使用显式常量,最好是收集在特殊文件和静态类中,或者是资源或数据文件.

只需准确地编写代码即可;它将在所有平台上运行.

此外,请正确使用异常处理.您可以在每个线程堆栈的最顶部捕获所有异常.同样,所有异常都应在主UI事件处理周期中捕获;每个UI库都有捕获它们的机制.您所做的就像在对自己犯下罪行:您正在捕获异常是某些功能并阻止其传播.永远不要做(很少有例外).您只需使异常保持沉默,即可有效地阻止强大的异常机制.您完全不应在此函数中使用异常块.如果您没有对自己犯下这种罪行,那么您已经很详细地了解了您的问题.另外,请使用调试器.

—SA
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


这篇关于在Windows 7中的vs2008中检索C#中的PC内容时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆