I am new to c# and I have a doubt about using a WinForm to complete a batch file with the arguments received by the form, execute the batch and create the specific files.
What I have:
WinForm -> 2 string variables (ip and user)
Batch file -> creating a .rdp file and its shortcut on the desktop with a personalized icon (the batch works when launched manually)
My problem is that the code works for the first time but if I try to change the variables the process doesn´t run and the files are not overwritten with the new information and I have an error saying I don´t have access.
WinForm code:
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());
}
}
Batch File Code
@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
I was thinking of launching the process code if the file didn´t exist and if exist overwriting it but I am not sure how to go from here.