【发布时间】:2019-01-17 18:28:16
【问题描述】:
我是 C# 的新手,我对使用 WinForm 完成带有表单接收到的参数的批处理文件、执行批处理并创建特定文件有疑问。
我有什么:
WinForm -> 2 个字符串变量(ip 和用户)
批处理文件 -> 在桌面上创建一个带有个性化图标的 .rdp 文件及其快捷方式(手动启动批处理时有效)
我的问题是代码第一次工作,但如果我尝试更改变量,进程不会运行并且文件不会被新信息覆盖,并且我有一个错误说我没有访问权限.
WinForm 代码:
private void ok_Click(object sender, EventArgs e)
{
string ipText, userText, defaultFile, rdpFile;
ipText = this.ipOutput.Text;
userText = this.userOutput.Text;
defaultFile = "C:\\TerminalServer\\RDbatch.cmd";
rdpFile = "C:\\TerminalServer\\RD Arbeitsplatz.rdp";
Console.WriteLine("IP : " + ipText + "; USER : " + userText);
Process p = null;
try
{
p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = defaultFile;
p.StartInfo.Arguments = String.Format("{0} {1}", ipText, userText);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
}
catch(Exception ex)
{
Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
}
}
批处理文件代码
@echo off
REM Change this by remming out desktop or all users desktop as you wish
REM Make sure that all entries below have " " around them as present
rem set Location="AllUsersDesktop"
set Location="C:\Users\Default\Desktop"
set DisplayName=""
set filename="C:\TerminalServer\RD Arbeitsplatz.rdp"
REM point to an ICO file or an icon within an existing EXE
rem set icon="C:\TerminalServer\rohwerderLogo.ico"
set icon="C:\TerminalServer\rohwerderLogo.ico, 0"
set WorkingDir="C:\TerminalServer"
del %filename% 2>NUL
(echo screen mode id:i:2
echo use multimon:i:0
echo desktopwidth:i:1920
echo desktopheight:i:1080
echo session bpp:i:32
echo winposstr:s:0,3,0,0,800,600
echo compression:i:1
echo keyboardhook:i:2
echo audiocapturemode:i:0
echo videoplaybackmode:i:1
echo connection type:i:7
echo networkautodetect:i:1
echo bandwidthautodetect:i:1
echo displayconnectionbar:i:1
echo disable wallpaper:i:0
echo allow font smoothing:i:0
echo allow desktop composition:i:0
echo disable full window drag:i:1
echo disable menu anims:i:1
echo disable themes:i:0
echo disable cursor setting:i:0
echo bitmapcachepersistenable:i:1
echo full address:s:%1
echo audiomode:i:0
echo redirectprinters:i:1
echo redirectcomports:i:0
echo redirectsmartcards:i:1
echo redirectclipboard:i:1
echo redirectposdevices:i:0
echo drivestoredirect:s:
echo username:s:%2
echo autoreconnection enabled:i:1
echo authentication level:i:2
echo prompt for credentials:i:0
echo negotiate security layer:i:1
echo remoteapplicationmode:i:0
echo alternate shell:s:
echo shell working directory:s:
echo gatewayhostname:s:
echo gatewayusagemethod:i:4
echo gatewaycredentialssource:i:4
echo gatewayprofileusagemethod:i:0
echo promptcredentialonce:i:0
echo gatewaybrokeringtype:i:0
echo use redirection server name:i:0
echo rdgiskdcproxy:i:0
echo kdcproxyname:s:
) > %filename%
attrib %filename% +r
echo Y|cacls %filename% /E /P %username%:r
REM Make temporary VBS file to create shortcut
REM Then execute and delete it
set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%USERPROFILE%\Desktop\RD Arbeitsplatz.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "C:\TerminalServer\RD Arbeitsplatz.rdp" >> %SCRIPT%
echo oLink.IconLocation = "C:\TerminalServer\rohwerderLogo.ico" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%
cscript /nologo %SCRIPT%
rem del %SCRIPT% 2>NUL
如果文件不存在并且如果存在覆盖它,我正在考虑启动流程代码,但我不知道如何从这里开始。
【问题讨论】:
-
您没有发布任何处理文件的 C# 代码,只是调用启动另一个进程。然后,其他进程会启动 另一个 脚本来尝试创建快捷方式。为什么不直接在 C# 代码中创建快捷方式?
-
无论如何,如果一个文件正在使用中,你不能覆盖它。您没有发布完整的异常,正如
Exception.ToString()返回的那样,因此只能猜测异常是什么或哪一行抛出了异常。完整的异常包含调用堆栈,它将显示哪个调用最终引发了异常 -
我怀疑这应该是Create a shortcut on desktop的副本
-
@PanagiotisKanavos 就像我说我是 c# 新手,我在决定使用 WinForm 之前创建了批处理文件。
-
没有例外,我们无能为力。 C# sn-p 中没有文件访问代码。
标签: c# winforms batch-file overwrite