Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Process
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;
            
            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 28-Jun-12 5:23am
Comments
Rajeev Jayaram at 28-Jun-12 11:43am
   
What is the error message and where exactly it breaks?
vantoora at 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()"
Damith Weerasinghe at 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 at 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 at 28-Jun-12 12:08pm
   
in your win2000 can you fing defreg.exe under c:\windows\system32\?
vantoora at 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 at 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 at 28-Jun-12 12:34pm
   
You may need to run it as administrator as well
vantoora at 28-Jun-12 14:13pm
   
already tried to run with administrator rights/account but cannot.

1 solution

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

Solution 1

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 :"
I changed it to " /A :" (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="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="*"
        />
 
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 498
1 OriginalGriff 439
2 ChintanShukla 305
3 Richard Deeming 250
4 RyanDev 210
0 Sergey Alexandrovich Kryukov 8,901
1 OriginalGriff 7,571
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 29 Jun 2012
Copyright © CodeProject, 1999-2014
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