Click here to Skip to main content
15,886,813 members
Please Sign up or sign in to vote.
3.00/5 (1 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)

C#
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;
            
            try
            {
                process.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            process.BeginErrorReadLine();
            process.BeginOutputReadLine();

            process.WaitForExit();

            _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
Comments
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
o)
à 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()"
DamithSL 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.
DamithSL 28-Jun-12 12:08pm    
in your win2000 can you fing defreg.exe under c:\windows\system32\?

1 solution

Hi,
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 :

XML
<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="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyidentity version="1.0.0.0" name="KBDefrag.app" />
  <trustinfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <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" />
      </requestedprivileges>
    </security>
  </trustinfo>
  
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- 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}"/>-->
      
    </application>
  </compatibility>
  
  <!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
  <dependency>
    <dependentassembly>
      <assemblyidentity>
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="*"
          publicKeyToken="6595b64144ccf1df"
          language="*"
        />
    </assemblyidentity></dependentassembly>
  </dependency>

</asmv1:assembly>


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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900