Click here to Skip to main content
13,044,467 members (58,892 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi folks,
I'm programming a little application to run "windows defragmentation" within c# code and then log the result and send it by mail.
I have finished to make it on my machine (running XP PRO SP3), but when I run it on a machine running Win2000 and Win2008 it throws error ("specified file not found")
I want to debug it but I cannot as I do not have visual studio on my Win2000 and Win2008.

Someone can point me to the right code please ? or tell me just why it throw an error.
Following is my code to call the process

cmdPath : "defrag"
parameter: "/A C:" (for example)

static void CheckDefrag(string cmdPath, string parameter)
            var process = new Process
                                StartInfo =
                                    FileName = cmdPath,
                                    Arguments = parameter,
                                    UseShellExecute = false,
                                    RedirectStandardOutput = true,
                                    RedirectStandardError = true,
                                    CreateNoWindow = true
                                EnableRaisingEvents = true
            process.ErrorDataReceived += ProcDataReceived;
            process.OutputDataReceived += ProcDataReceived;
            catch (Exception ex)
            _dateTimeEnd = DateTime.Now.Year
                         + DateTime.Now.Month.ToString("d2")
                         + DateTime.Now.Day.ToString("d2") + "_"
                         + DateTime.Now.Hour.ToString("d2")
                         + DateTime.Now.Minute.ToString("d2")
                         + DateTime.Now.Second.ToString("d2");
            WriteLogContent.WriteLogContents(_dateTimeEnd + "\n" + @"=<=>=<=>=<=<=>=<=>=<=<=>=<=>=<=<=>=<=>=<=<=>=<=>=<=<=>=<=>=<=>=<=>=<=<=>=<=>=<=<=>=<=>=<=<=", _logFileName);
Posted 28-Jun-12 5:23am
Rajeev Jayaram 28-Jun-12 11:43am
What is the error message and where exactly it breaks?
vantoora 28-Jun-12 11:56am
The error message is :

Exception non gérée : System.ComponentModel.Win32Exception: Le fichier spécifié
est introuvable
à System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInf
à System.Diagnostics.Process.Start()
à KBDefrag.Program.CheckDefrag(String cmdPath, String parameter)
à KBDefrag.Program.Main(String[] args)

(it's in french and means "specified file not found")

And it breaks after trying to execute "process.Start()"
Damith Weerasinghe 28-Jun-12 11:49am
how you set cmdPath? Note that you need to locate defrag.exe in cmdPath. check that it is same as in XP for both Win2000 and Win2008. there is a different in 32bit and 64bit os as well
vantoora 28-Jun-12 11:57am
cmdPath is just a string of "defrag".
Have tried with a fullpath like "c:\windows\system32\defrag.exe" but still remain the same error.
Damith Weerasinghe 28-Jun-12 12:08pm
in your win2000 can you fing defreg.exe under c:\windows\system32\?
vantoora 29-Jun-12 3:57am
No do not have the command defrag.exe under win2000 (c:\winnt\system32)
It's "dfrg.msc", and modified my code according to this to just open the window of the defragmentation app but still having the same error.
vantoora 29-Jun-12 4:01am
Already checked and XP Win2008 have the same cmdPath but only the option differs.
For Windows 2008 option have "/" instead of "-" but tried to run in dos window and still can run even with "-" (e.g. defrag -a c: and defrag /A c: both for XP and 2008).

And the strange thing I just pointed out this morning too is that using the same code, no modification made but just changed the cmdPath into "chkdsk" (check disk) instead of "defrag" it's running well.
Damith Weerasinghe 28-Jun-12 12:34pm
You may need to run it as administrator as well
vantoora 28-Jun-12 14:13pm
already tried to run with administrator rights/account but cannot.

1 solution

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

Solution 1

I think I found a solution for my problem.
It seems to work but I'm still testing.
Following are the things I made, in case someone else will encounter similar problem :
1. for the "StartInfo.Arguments" argument was : "/A <driveletter>:"
I changed it to " /A <driveletter>:" (with the space before)
2. I created a manifest app file inside my project as guided through this link[^]
and here is my manifest file :

<asmv1:assembly manifestversion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="">
  <assemblyidentity version="" name="" />
  <trustinfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <requestedprivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
            If you want to change the Windows User Account Control level replace the 
            requestedExecutionLevel node with one of the following.
        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />
            Specifying requestedExecutionLevel node will disable file and registry virtualization.
            If you want to utilize File and Registry Virtualization for backward 
            compatibility then delete the requestedExecutionLevel node.
        <requestedexecutionlevel level="highestAvailable" uiaccess="false" />
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
      <!-- A list of all Windows versions that this application is designed to work with. Windows will automatically select the most compatible environment.-->
      <!-- If your application is designed to work with Windows 7, uncomment the following supportedOS node-->
      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>-->
  <!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->

Ok then, I'll write back here if my application is running smoothly next week as I'm going to test it this week-end.

Thanks for all of you.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 29 Jun 2012
Copyright © CodeProject, 1999-2017
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