I try to execute a .cmd to export files from SVN, it works manually and in my local machine(WS2003 SP2 IIS Identiy: Local System), but it fails in the execution server (same configuration).
The script I execute is:
echo.%Date%-%Time%- Username %username%>>c:\vpcheckcode\logsvn.txt
svn export %svnUrl% %srcFolder% --force --username %usr% --password %psw%
If I try simply to copy a file, no svn, it works.
When I execute it manually it logs my user and it exports the files.
On develop PC it doesn't log the user but it works, in execution server it doesn't log the user and it doesn't works.
I try to execute the .cmd in this way:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = strFileName;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = svnUrl + " " + srcFolder+ " " + usr+" "+psw;
using (Process exeProcess = Process.Start(startInfo))
{
pid = exeProcess.Id;
EventLog.WriteEntry("VPCheckCodeSrvDll.ExecuteScriptSVN", "avviato pid svn: " + pid.ToString(), EventLogEntryType.Warning);
if (!exeProcess.WaitForExit((int)_svnExportTimeout))
{
exeProcess.Kill();
The process hangs untill it has been killed.
C:\Documents and Settings\myuser>tlist 4980
4980 cmd.exe
CWD: c:\windows\system32\inetsrv\
CmdLine: cmd /c ""C:\ANALISI\000000-xx-v90\SVN\svnExport.cmd" https://svn.mysite.local/repos/HA/branches/H
VirtualSize: 11332 KB PeakVirtualSize: 15492 KB
WorkingSetSize: 1832 KB PeakWorkingSetSize: 1848 KB
NumberOfThreads: 1
2704 Win32StartAddr:0x4ad07670 LastErr:0x000000cb State:Waiting
5.2.3790.3959 shp 0x4AD00000 cmd.exe
5.2.3790.4937 shp 0x7C800000 ntdll.dll
5.2.3790.5069 shp 0x77E40000 kernel32.dll
7.0.3790.3959 shp 0x77BA0000 msvcrt.dll
5.2.3790.4455 shp 0x7D1E0000 ADVAPI32.dll
5.2.3790.4759 shp 0x77C50000 RPCRT4.dll
5.2.3790.4455 shp 0x76F50000 Secur32.dll
5.2.3790.4033 shp 0x77380000 USER32.dll
5.2.3790.4396 shp 0x77C00000 GDI32.dll
5.2.3790.3959 shp 0x71BD0000 MPR.dll
5.2.3790.3959 shp 0x76290000 IMM32.DLL
Any suggestion?
I did something similar. Try the following:
Change : startInfo.Arguments = svnUrl + " " + srcFolder+ " " + usr+" "+psw;
to : startInfo.Arguments = String.Format("{0} \"{1}\" --username {2} --password {3}", svnUrl, srcFolder, usr, psw);
I am making the assumption that your usr and psw variables do not contain the parameter information for subversion. Also, my destination included the file name (I can't see your full command line as it is cut off) and also contained spaces, so I had to enclose it within quotes. Your svnUrl should have "%20" in place of spaces if you have any (again, the full URL is not visible).
Related
I've looked through the internet on how winrar's command line parameters work, and this is what I have so far
void LOCK(string fld, string pw)
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Program Files\WinRAR\WinRAR.exe";
p.StartInfo.Arguments = "rar a -p" + pw + " PL_LOCKED_ARCHIVE.rar " + fld;
p.Start();
}
void UNLOCK(string fld, string pw)
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Program Files\WinRAR\WinRAR.exe";
p.StartInfo.Arguments = "unrar x -p" + pw + " PL_LOCKED_ARCHIVE.rar";
p.Start();
}
However it doesn't seem to create any archive anywhere, with a test folder being C:\PicsAndStuff
The StartInfo you define results in running WinRAR.exe with command line:
C:\Program Files\WinRAR\WinRAR.exe unrar x -p pw PL_LOCKED_ARCHIVE.rar
That is of course wrong as you do not want to run WinRAR.exe with first argument being a reference to console version Rar.exe or UnRAR.exe. The result is most likely an error message because of invalid command rar respectively unrar as the first argument must be a or x for WinRAR.exe.
So first of all you need to correct StartInfo:
void LOCK(string fld, string pw)
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Program Files\WinRAR\Rar.exe";
p.StartInfo.Arguments = "a -p" + pw + " PL_LOCKED_ARCHIVE.rar " + fld;
p.Start();
}
void UNLOCK(string fld, string pw)
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Program Files\WinRAR\UnRAR.exe";
p.StartInfo.Arguments = "x -p" + pw + " PL_LOCKED_ARCHIVE.rar";
p.Start();
}
Further all commands and switches of console version Rar.exe are briefly explained when simply running Rar.exe without any parameter in a command prompt window. Also UnRAR.exe outputs a brief help if executed without any parameter.
Last but not least there is a complete manual for Rar.exe which of course can also extract files and folders from a RAR archive which makes additional usage of UnRAR.exe useless. The manual is text file Rar.txt in program files folder of WinRAR which you should read from top to bottom. I suggest to build the command line while reading it and test the command line first from within a command prompt window.
Note 1:
Rar.exe is shareware. Only UnRAR.exe is freeware.
Note 2:
GUI version WinRAR.exe supports more than console version Rar.exe and therefore the list of switches differ slightly. Complete documentation for WinRAR.exe can be found in help of WinRAR opened with Help - Help Topics or pressing key F1. Open in help on tab Contents the item Command line mode and read. WinRAR.exe is also shareware.
You need to encrypt both file data and headers.
According to Documentation (Command line mode > Switches > "-hp[pwd] - encrypt both file data and headers"):
This switch is similar to -p[p], but switch -p encrypts only file data
and leaves other information like file names visible. This switch
encrypts all sensitive archive areas including file data, file names,
sizes, attributes, comments and other blocks, so it provides a higher
security level.
This is how you can access to it using command line:
Syntax: rar a -hp[MyPassword] -r [filepath] [folderpath]
"C:\Program Files\WinRAR\WinRAR.exe" a -hp12345678 -r d:\zipProject d:\Project
C# Code:
void LOCK(string fld, string pw)
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Program Files\WinRAR\WinRAR.exe";
p.StartInfo.Arguments = "rar a -hp" + pw + " PL_LOCKED_ARCHIVE.rar " + fld;
p.Start();
}
I read many post, from them there is this one
c# - Opening the terminal process and pass commands?
I do the exact same thing in my code
Process proc = new System.Diagnostics.Process ();
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments = "-c \" " + command + " \"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start ();
where command = export DISPLAY=:0.0
and it goes to my catch, "pplicationName='/bin/bash', CommandLine='-c " cd .. "', CurrentDirectory='', Native error= The system cannot find the file specified."
what do I do differently? even if I try to juste set command = "cd .." it doesn't work
You should probably try setting the full path the executable.
proc.StartInfo.FileName = "C:/SOMEPATH/Bash.exe";
I'm assuming as you are specifying a relative path, it's not resolving it. Possibly because you aren't setting a working directory for the process so it's current dir and the current dir you think it has, are different.
I was wondering whether it is possible to unzip files in c sharp without using open source dll's in .net 4.0 or lower?
I have some VBA code (below) that uses the "Shell" command. Is that also possible from c sharp?
Sub UnzipMe(path)
Dim strDOSCMD As String
Dim filename As String
Dim i As Integer
filename = path + "\test.txt"
strDOSCMD = "unzip -n " + path + "\zipfile.zip -d " + path
'SEND TO DOS
retval = Shell(strDOSCMD, vbHide)
End Sub
This works fine and is very simple but I would like to do it all in c sharp and not mix and match. Surely that should be doable or there should be an equally simple solution?
You can start a process in C# using Process.Start.
Your code can look like (not tested..):
public void UnzipMe(string path){
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C unzip -n " + path + "\zipfile.zip -d " + path;
process.StartInfo = startInfo;
process.Start();
//do some extra stuff here
}
For zip stuff consider to use a third party library like sharpziplib I use it with success in many projects.
Take a look to these samples: https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples
so I have a small chunk of code that detects files in a folder and systematically zips them after they become a certain age. I'm now working on a piece of code to unzip files of a certain date-range at user request for use in the software.
My issue is that the command-line string to zip files works great, but the unzip does not... below is the excerpt of code showing how I unzip, please let me know what I should do differently to ensure unzipping. thanks!
private void UnZipFile()
{
if (myRecord == null)
{
if (File.Exists(zipInfo.FullName))
{
Process LogUnzipper = new Process();
//32-bit system
if (File.Exists("c:\\Program Files\\WinZip\\WZZIP.exe"))
{
//WZZIP.exe being the WinZip executable
LogUnzipper.StartInfo.FileName = "c:\\Program Files\\WinZip\\WZZIP.exe";
}
//64-bit system
else if (File.Exists("c:\\Program Files (x86)\\WinZip\\WZZIP.exe"))
{
//WZZIP.exe being the WinZip executable
LogUnzipper.StartInfo.FileName = "c:\\Program Files (x86)\\WinZip\\WZZIP.exe";
}
//here is where I think I'm screwing up something..
string action = "-e " + "\"" + zipInfo.FullName + "\"" + " \"" + zipInfo.DirectoryName + "\"";
//happen in background
LogUnzipper.StartInfo.CreateNoWindow = true;
LogUnzipper.StartInfo.UseShellExecute = false;
LogUnzipper.StartInfo.Arguments = action;
LogUnzipper.Start();
while (!LogUnzipper.HasExited)
{
LogUnzipper.WaitForExit(500);// 1/2 sec
}
//adding break point at this line yields no unzipped Log file :(
}
...
my thoughts are that I'm somehow calling the cmd wrong in string action? even though if I test it in a windows command prompt that's correct formatting.
***it should be noted that ZipInfo.FullName is something along the ex: "C:\Users\16208\Software\Beta\logs\6_2013\Log_10AM_to_11AM.zip" as far as formmat, so I am giving an accurate path to the zipped Item.
You can use some free and open-source .Net library for zipping and unzipping (as SLaks suggested). For example DotNetZip.
using (ZipFile decompress = ZipFile.Read(ZipFilePath))
{
foreach (ZipEntry e in decompress)
{
e.Extract(TargetPath, ExtractExistingFileAction.OverwriteSilently);
}
}
As for your code, waiting half a second may not be enough for unzipping to complete. Also you can try running your unzip command in a command line and check the output.
If you have WinZip installed try this:
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", #"/c start winzip32 -e " + fileName + #" C:\SomeDirectory");
rocStartInfo.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
Where fileName is the full path to your *.rar file.
I tried various methods possible to run this specific DOS command through C#. I do not want to use a batch file. Whatever I try, it keeps taking only first word from Printer name and not the entire name, in this case, it says Printer POS is not connected instead of saying Printer POS Lexmark is not connected. What could the error be? Thanks guys!
The DOS command is:
rundll32 printui, PrintUIEntry /o /n "POS Lexmark"
My code is as follows:
string command = string.Format("/c rundll32 printui, PrintUIEntry /o /n" + " POS Lexmark");
ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");
cmdsi.Arguments = command;
cmdsi.CreateNoWindow = false;
Process cmd = Process.Start(cmdsi);
You forgot to include the quotes around POS Lexmark:
string command = string.Format("/c rundll32 printui, PrintUIEntry /o /n" + "\" POS Lexmark\"");