调用进程以运行Windows碎片整理 [英] Calling process to run windows defragmentation

查看:95
本文介绍了调用进程以运行Windows碎片整理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在编写一个小应用程序,以在C#代码中运行"Windows碎片整理",然后记录结果并通过邮件发送.
我已经完成了在计算机上运行XP PRO SP3的操作,但是在运行Win2000和Win2008的计算机上运行它时会抛出错误(找不到指定的文件")
我想调试它,但不能,因为我在Win2000和Win2008上没有Visual Studio.

有人可以给我指出正确的代码吗?或告诉我为什么会引发错误.
以下是我调用该过程的代码

cmdPath:碎片整理"
参数:"/A C:"(例如)

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

推荐答案


我想我找到了解决问题的方法.
似乎可行,但我仍在测试中.
以下是我所做的操作,以防其他人遇到类似的问题:
1."StartInfo.Arguments"参数为:"/A< driveletter> ;:"
我将其更改为"/A< driveletter>:"(前面有空格)
2.按照此链接的指导,我在项目内部创建了清单应用文件. [^ ]
这是我的清单文件:

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 :

<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.


这篇关于调用进程以运行Windows碎片整理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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