VS软件默认安装路径修改、CreateFile返回值为0xffffffff、处理文件-File失败,不具有.ps1扩展名、调用有管理员权限的cmd、无法正常运行.exe且无返回值、.bat管理员权限

本次软件整理遇到的问题整理

1、打包默认安装路径有空格怎么办

打包的默认安装路径在解决方案--打包项目(**Setup)--右键--view-文件----Application Folder--属性,DefaultLocation默认为[ProgramFiles64Folder][Manufacturer][\ProductName],其中:ProgramFiles64Folder]默认C盘的program files.

将默认安装路径改为如下形式:C:\SUNG

debug目录下的文件就安装到以下目录。

C:\WINDOWS

2使用HID.cs发现,若不先使用FindUSBDevice(),直接OpenDevice(),若要打开一个不存在的设备后,再次打开存在的设备,在方法:

device = CreateFile(deviceList[i], DESIREDACCESS.GENERIC_READ | DESIREDACCESS.GENERIC_WRITE, 0, 0, CREATIONDISPOSITION.OPEN_EXISTING, 0x40000000, 0);

这里的返回值都为0xffffffff;

所以,在打开设备之前,必须查看设备(FindUsbDevice)是否存在,不可以直接打开(OpenDevice)一个不存在的设备。

其他工具可能与createfile(对设备的属性值进行修改访问)相冲突,若此问题出现,则在设备管理器中读取到的DeviceList()中将不能访问属性的设备去掉,这样就不是createfile这个设备了。

                //这里要注意deviceList是随时变化的,遍历要根据其值的改变而改变
                for (int i = 0; i < deviceList.Count; )
                {
                    if(deviceList[i].IndexOf("vid_****&pid_****")>-1)
                    {
                        deviceList.RemoveAt(i);
                        //break;
                    }
                    else
                    {
                        i++;
                    }
                }

                for (int i = 0; i < deviceList.Count; i++){...
                }

3现象:处理文件-File "C\Program" -program file 失败,因为该文件不具有.psl扩展名,请指定一个有效的路径。

软件中调用了一个.bat文件,在软件中可以正确使用。但在安装软件时,采用默认路径安装到“C:\Program Files”中时,调用.bat文件失败,提示在“C:\Program”中找不到对应的.ps1文件。

原因:.bat文件无法识别路径中有空格

解决方案:按照上面的方法,修改软件的默认安装路径,将其安装到没有空格的路径中即可。

4 以管理员权限调出cmd窗口,并且可以输入命令运行。

先读取readme.txt中的内容显示到log中,在打开的cmd窗口中可以复制过去运行指令。

        private void button_download_Click(object sender, EventArgs e)
        {
            FileOperation file_opt = new FileOperation();
            richTextBox1.Text += file_opt.Read(".\\readme.txt");
            richTextBox1.Text += "\n";

            //调用cmd窗口,以管理员权限
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/c C:\\Windows\\System32\\cmd.exe";
            startInfo.UseShellExecute = false;
            startInfo.Verb = "RunAs";

            Process p = new System.Diagnostics.Process();
            p.StartInfo = startInfo;

            p.Start();
        }

其中用到的函数

    public class FileOperation
    {
        public string Read(string path)
        {
            StreamReader sr = new StreamReader(path);
            string strContent = sr.ReadToEnd();
            sr.Close();
            return strContent;
        }
    }

..

5 调用进程,运行.exe工具。

        private void GetConID()
        {
            Process p = new Process();

            p.StartInfo.FileName = ".\\Tool\\GConID.exe";

            p.StartInfo.Arguments = "GetConID";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.WorkingDirectory = Path.GetDirectoryName(p.StartInfo.FileName);
            p.StartInfo.Verb = "runas";

            p.StartInfo.CreateNoWindow = true;

            p.Start();
            string result = p.StandardOutput.ReadToEnd();

            if (result=="")
            {
                MessageBox.Show("ContainerID is empty");
            }
            else
            {
                //处理conID字符,这里的字符在{}之间,以-隔开,只需要保留字符即可
                int fir = result.IndexOf("{", 0);
                int end = result.IndexOf("}", 0);
                string conID = result.Substring(fir + 1, end - fir - 1);
                conID = conID.Replace("-", "");
                form_log.richTextBox_logout.Text += "ConID: " + conID + "\n\n";
                Number_ContainerID.Text = containerID;
            }
            
            p.WaitForExit();
            p.Close();
        }

其中遇到文件,在调用其他.exe时,没有增加

p.StartInfo.WorkingDirectory = Path.GetDirectoryName(p.StartInfo.FileName);

p.StartInfo.Verb = "runas";

但是运行时无法正常运行,并且也无法得到返回值,增加这两句话后即可。

6 编写.bat文件,使其在管理员权限下运行指令。

整个.bat文件的格式类似如下:

cls
 @echo off
 title 获取管理员权限
mode con cols=100 lines=20
 color 3f


:: 开始获取管理员权限
setlocal
 set uac=~uac_permission_tmp_%random%
 md "%SystemRoot%\system32\%uac%" 2>nul
 if %errorlevel%==0 ( rd "%SystemRoot%\system32\%uac%" >nul 2>nul ) else (
     echo set uac = CreateObject^("Shell.Application"^)>"%temp%\%uac%.vbs"
     echo uac.ShellExecute "%~s0","","","runas",1 >>"%temp%\%uac%.vbs"
     echo WScript.Quit >>"%temp%\%uac%.vbs"
     "%temp%\%uac%.vbs" /f
     del /f /q "%temp%\%uac%.vbs" & exit )
 endlocal
:: 完成获取,下面可以开始写你自己的代码了


cd /d %~dp0  //到达当前目录 /d
::echo %cd%\  //显示当前路径
::echo cd /d %%~dp0

Echo **Start Update**
cd prog   //到达prog文件夹下目录
ping -n 1 127.0.0.1 >nul //与设备ping通信
ping -n 1 127.0.0.1 >nul
prog.exe -fw %cd%\..\Firmware\Oasis\firmware\Factory\Oasis.img -dest SYSTEM  //运行指令
ping -n 2 127.0.0.1 >nul
cd ..  //返回上一层
cd tek  //到达下一层tek文件夹
ping -n 2 127.0.0.1 >nul
Caling.bat -BoardRev "RevA" -PresenceVid 0x04E8  -PresencePage

7、在管理员权限下运行.bat脚本,若参数为相对路径的某个文件,提示参数无效。

例如:prog.exe -fw  %cd%\..\Fware\Oas\fware\Factory\Oasis.ini -dest SYSTEM

在这里使用了%cd%,采用决定路径,使参数为有效参数。

8、管理员权限调用cmd.exe,并运行exe文件

            调用cmd窗口,以管理员权限
            ProcessStartInfo startInfo = new ProcessStartInfo();
            //startInfo.FileName = ".\\Tools\\cleanup\\cleanup.cmd";
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/c C:\\Windows\\System32\\cmd.exe";
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            //startInfo.CreateNoWindow = true;
            startInfo.Verb = "RunAs";

            Process p = new System.Diagnostics.Process();
            p.StartInfo = startInfo;

            p.Start();
            p.StandardInput.WriteLine(".\\Tools\\cleanup\\cleanup.cmd");//加上p.StandardInput.WriteLine("exit");运行框会消失
            p.StandardInput.AutoFlush = true;


            string result = p.StandardOutput.ReadToEnd();
            richTextBox_logout.Text += result + "\n\n";

9、设置label控件的背景为透明,并设置其位置。

选中控件labe1,将其BackColor设置为Transparent

并且在代码中,label1.Parent=picture_Background;//这里的背景色是整个UI的背景色。

label.Location=new Point(12,34); //控件原本的属性中有原来的位置,可以参考进行调整。

..

.

猜你喜欢

转载自blog.csdn.net/yanhuatangtang/article/details/80404097
今日推荐