开发者

List Drives using a WCF Service in Silverlight C#

开发者 https://www.devze.com 2023-02-19 14:46 出处:网络
Trying to get a list of drives from a server side application using a WCF Service and i keep getting an error/ no drives found. Here is my Server side code. Hoping someone could shed some light on thi

Trying to get a list of drives from a server side application using a WCF Service and i keep getting an error/ no drives found. Here is my Server side code. Hoping someone could shed some light on this never ending "Access" issues with silverlight!

[OperationContract]
    public List<AllDriveInfo> ServerDriveInfo()
    {
        try
        {
            DriveInfo[] ComputerDrives = DriveInfo.GetDrives();
            //List<DriveInfo> ComputerDrives = DriveInfo.GetDrives();
            //string[] ComputerDrives = Environment.GetLogicalDrives();
            List<AllDriveInfo> ServerDrives = new List<AllDriveInfo>();

            foreach (DriveInfo drive in ComputerDrives)
            {
                AllDriveInfo NewDrive = new AllDriveInfo();
                NewDrive.DriveLetter = drive.VolumeLabel;
                NewDrive.VolumeName = drive.Name;
                ServerDrives.Add(NewDrive);
            }
            return ServerDrives;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

I've recently also tried a different approach by using "InteropServices" with the code stated below. However WCF seems to not have any name space for the AutomationFactory object which is normally the refrence:

using System.Runtime.InteropServices.Automation;

[OperationContract]
        public List<AllDriveInfo> ServerDriveInfo()
        {

            dynamic fileSystem = AutomationFactory.CreateObject("Scripting.FileSystemObject");
            dynamic drives = fileSystem.Drives;
            List<AllDriveInfo> Drives = new List<AllDriveInfo>();

            foreach (var drive in drives)
            {
                try
                {
                    Drives.Add(new AllDriveInfo
                    {
                        VolumeName = drive.VolumeName,
                        DriveLetter = drive.Dr开发者_JAVA百科iveLetter,
                    });
                }
                catch (Exception ex) { }
            }
           return Drives;
        }


So i Finally Figure it out and the problem was that some drives have no Volume Name, Total Space or Free Space so it would just pukes on itself! Below is my working sample that is working flawlessly. :D

[OperationContract]
        public List<AllDriveInfo> ServerDriveInfo()
        {
            try
            {
                DriveInfo[] ComputerDrives = DriveInfo.GetDrives();
                List<AllDriveInfo> ServerDrives = new List<AllDriveInfo>();
                foreach (DriveInfo drive in ComputerDrives)
                {
                    AllDriveInfo NewDrive = new AllDriveInfo();

                    NewDrive.DriveLetter = drive.Name.ToString();

                    try { NewDrive.VolumeName = drive.VolumeLabel.ToString(); }
                    catch (Exception ex) { NewDrive.VolumeName = " "; }

                    try { NewDrive.TotalSpace = Convert.ToDouble(drive.TotalSize) / Math.Pow(1024, 3); }
                    catch (Exception ex) { NewDrive.TotalSpace = 0; }

                    try { NewDrive.FreeSpace = Convert.ToDouble(drive.TotalFreeSpace) / Math.Pow(1024, 3); }
                    catch (Exception ex) { NewDrive.FreeSpace = 0; }


                    ServerDrives.Add(NewDrive);
                }
                return ServerDrives;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消