Start MS DOS command "attrib" as process (C#) - c#

I'm trying to hide a folder with C# using the MSDOS "attrib" command.
For now i'm able to do that by writing the "attrib" command + arguments in a batch file, running that file using Process.Start(), and then deleting it. I was wondering, can I do that directly from C#?
Here is what i've tryed so far... (the code below doesen't work)
public static void hideFolder(bool hide, string path)
{
string hideOrShow = (hide) ? "+" : "-";
Process.Start("attrib " + hideOrShow + "h " + hideOrShow + "s \"" + path + "\" /S /D");
}
Any help would be appriciated!
Thanx!

What you asked for:
string hideOrShow = (hide) ? "+" : "-";
Process.Start("cmd /c attrib " + hideOrShow + "h " + hideOrShow + "s \"" + path + "\" /S /D");
What you should do instead:
File.SetAttributes(path, FileAttributes.Hidden);

The first parameter to Process.Start() needs to be the name of an executable file or document. You'll need to pass in two parameters, like this:
Process.Start("attrib.exe", hideOrShow + "h " + hideOrShow + "s \"" + path + "\" /S /D");
Also, while attrib.exe will work when called directly, most people will pass this kind of DOS-style command to the command interpreter (which will also work for built-in commands, etc.)
Process.Start("cmd.exe", "/c attrib " + restOfTheArguments);

C# makes this really easy - the idea is you get the files current attributes (File.GetAttributes()), then you add in the Hidden attribute before calling File.SetAttributes()
check the below out, it'll make c:\blah hidden
static void Main(string[] args)
{
FileAttributes oldAttributes = File.GetAttributes(#"c:\blah");
File.SetAttributes(#"c:\blah", oldAttributes | FileAttributes.Hidden);
}
to remove the hidden attribute you need to remove the hidden attribute
static void Main(string[] args)
{
FileAttributes newAttributes = File.GetAttributes(#"c:\blah");
newAttributes = newAttributes & (~FileAttributes.Hidden);
File.SetAttributes(#"c:\blah", newAttributes);
}

What's the error? Why not use http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.attributes.aspx ?

Related

Postman - how to pass data from List<string>, in memory, into -data portion of Newman command line

I need to execute Postman collection from within my C# method.
But instead of the data file, I need to pass the data directly from the method output (as List).
Here is my code:
public StringBuilder RunPostmanCall(string collectionPath, string executionFolder, string environmentPath, List<string> inputFilePath = null)
{
StringBuilder runOutputBuilder = new StringBuilder();
string runOutput = null;
ProcessStartInfo psiNpm = new ProcessStartInfo
{
FileName = "cmd",
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false
};
Process pNpmRun = Process.Start(psiNpm);
pNpmRun.OutputDataReceived += (sender, a) => runOutputBuilder.AppendLine(a.Data);
Console.WriteLine(" - Install Newman ...");
pNpmRun.StandardInput.WriteLine($"npm install -g newman");
Console.WriteLine(" - Execute Postman Script ...");
string value = $"newman run " +
$"\"" + collectionPath + "\" " +
$"--folder \"" + executionFolder + "\" " +
$"--environment \"" + environmentPath + "\" " +
$"-d \"" + inputFilePath + "\" " +
$"--disable-unicode";
pNpmRun.StandardInput.WriteLine(value);
pNpmRun.BeginOutputReadLine();
pNpmRun.StandardInput.WriteLine("exit 0");
I'm getting the following error:
bin\Debug>newman run "../../api/postman_audit.json" --folder "SearchIndex" --environment "../../api/postman_environment.json" -d "System.Collections.Generic.List`1[System.String]" --disable-unicode
I could save the output into the file, and then just use that file location in the command-line. But I would like to avoid creating a file, and read data directly from the memory.
unfortunately with -d you can only refer to a file in your filesystem.
If you are not willing to write this data directly into a file, i would suggest to set the values directly as a global variable from commandline.
Try to add this parameter to your newman run command
--global-var key=value
You can add your data into a string and add it as a global variable. You can parse it in your pre-request or test-script normally.

c# Launch process without taking focus

I've created a GUI app that basically loads a video file, launches 3 processes with arguments in sequential order, (ffmpeg, x264, mp4box) and has the ability to abort if needed.
I'm trying to figure out a way to launch these processes without them stealing focus from whatever program is opened. Not necessarily the original form itself...it could be IE, MS WORD, etc...I know about ProcessWindowStyle, however, all options still take focus away. I do not want to start these processes hidden either.
This is an example of my code for starting a process
Process ffmpegProcess = new Process();
ffmpegProcess.StartInfo.FileName = Settings.Default.ffmpeg;
ffmpegProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
string outputaud = text2 + "_audio.aac";
string AudioBitrate = this.cBAudiobitrate.GetItemText(this.cBAudiobitrate.SelectedItem);
string ffargs = "-i " + quote + SourceFile + quote + " -filter_complex " + quote + "[0:2][0:3]amerge=inputs=2,pan=stereo|c0=c0+c1|c1=c0+c1[aout]" + quote + " -map " + quote + "[aout]" + quote + " -strict experimental -acodec aac -b:a " + AudioBitrate + "k " + quote + outputaud + quote;
ffmpegProcess.StartInfo.Arguments = ffargs;
ffmpegProcess.Start();
while (!ffmpegProcess.HasExited)
{
if (this.bw404p_withaudio.CancellationPending)
{
ffmpegProcess.Kill();
e.Cancel = true;
return;
}
Thread.Sleep(1000);
}
I know about using the Microsoft.VisualBasic Reference and Shell command with AppWinStyle as it has options for launching with no focus, I just can't seem to figure out how to modify my code to make it work with that.
Any ideas?
Thanks
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Is that would you're looking for? More info below
How to start Process hidden?
http://www.dotnetperls.com/process-start
ProcessWindowStyle Enumeration

File.Copy from local to shared folder in server

I'm having problems when copying a .txt from a local server ("D:\AuditFiles") to a shared folder in another server ("\\PrintServer\SharedFolder"). It throws the exception:
"The filename, directory name, or volume label syntax is incorrect."
I thought it could be something with the path format, so I tried by adding to the server path an #:
#Configuration.Manager["Path"] | #"\\ServerPath\SharedFolder"
I've also tried with this format: \\ServerPath\SharedFolder... None of them worked.
By the way its not an access problem, cause i've tried to do the same thing running a command prompt from within c#:
System.Diagnostics.Process.Start("cmd.exe", "/C COPY PATH1, PATH2"); //This worked and copied the file.
I'd be greatful if someone could give me a clue of what could be the problem here. At least an advice of what to do.
Thanks in advance and sorry me bad english!
Edit:
This is the part of the code that should work:
string pathPrevDay = "D:\AuditFiles\enc_" + svr.Name + "_counts" + day.AddDays(-1).ToString("dd-MM-yy") + ".txt";
if(File.Exists(pathPrevDay))
{
File.Copy(pathPrevDay, #ConfigurationManager.AppSettings["MAIL_SERVER_PATH"]);
}
You need to escape the backslashes and indicate the name of the file in File.Copy(pathPrevDay, ConfigurationManager.AppSettings["MAIL_SERVER_PATH"]);.
Change this:
string pathPrevDay = "D:\AuditFiles\enc_" + svr.Name + "_counts" + day.AddDays(-1).ToString("dd-MM-yy") + ".txt";
to this:
string pathPrevDay = "D:\\AuditFiles\\enc_" + svr.Name + "_counts" + day.AddDays(-1).ToString("dd-MM-yy") + ".txt";
You can also use single slashes (/) instead like this:
string pathPrevDay = "D:/AuditFiles/enc_" + svr.Name + "_counts" + day.AddDays(-1).ToString("dd-MM-yy") + ".txt";

Enreco Video Rotation in Asp.Net Virtual Machine is not completing

I am developing an ASP.NET Web Api in which I need to concatenate some video clips and rotate them. I could achieve the same when I tried in my local system. When I deployed the same project to an Azure Virtual Machine I am not getting response. I am pretty sure that there isn't any issue till video concatenation because I could see the concatenated video in the expected folder. Here is the code snippet.
var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
NReco.VideoConverter.ConcatSettings set = new NReco.VideoConverter.ConcatSettings();
ffMpeg.GetVideoThumbnail(_fileNames[0], imageRootPath + tobename + ".jpg");
if (_fileNames.Count() > 1)
{
ffMpeg.ConcatMedia(_fileNames, videoRootPath + tobename + "_r.mp4", NReco.VideoConverter.Format.mp4, set);
string path = HttpContext.Current.Server.MapPath("~\\bin\\");
System.Diagnostics.Process ffmpeg = new System.Diagnostics.Process();
ffmpeg.StartInfo.FileName = path + "\\" + "ffmpeg.exe";
ffmpeg.StartInfo.Arguments = "-i " + videoRootPath + tobename + "_r.mp4" + " -c copy -metadata:s:v:0 rotate=90 " + videoRootPath + tobename + ".mp4";
ffmpeg.Start();
ffmpeg.WaitForExit();
}
ffmpeg.ConcateMedia is working fine. I can't figure out why the External process that I have invoked does not complete. The same piece is working fine in my local Visual Studio.
Thank you in advance
It seems you are using Nreco VideoConvertor for joinging videos and external process to rotate the video.
You can always use Invoke method to write the custom commandline. something like this
ffMpeg.Invoke("-i " + videoRootPath + tobename + "_r.mp4" + " -c copy -metadata:s:v:0 rotate=90 " + videoRootPath + tobename + ".mp4");
Hope this Helps...
Your path ends with a slash, and when adding the paths together you also add a slash.
Use Path.Combine:
string path = HttpContext.Current.Server.MapPath("~\\bin");
ffmpeg.StartInfo.FileName = System.IO.Path.Combine(path, "ffmpeg.exe");

Java (Minecraft) doesnt start from c#

When I copy paste the javaw.exe -arguments to console it works but, when I launch it like this it doesn't work.
string directory = "C:\\Users\\Can\\AppData\\Roaming\\.minecraft";
string java = #"C:\windows\system32\javaw.exe";
string javaLocation = "C:\\Program Files\\Java\\jre7\\bin\\javaw.exe";
string RAM = "1G";
string username = "namehere";
string token = "--session token:"+tokenGenerated;
string version = "1.6.2";
string launch = "-Xmx" + RAM + " -Djava.library.path={0}\\versions\\1.6.2\\1.6.2-natives-7453523379463 -cp {0}\\libraries\\net\\sf\\jopt-simple\\jopt-simple\\4.5\\jopt-simple-4.5.jar;{0}\\libraries\\com\\paulscode\\codecjorbis\\20101023\\codecjorbis-20101023.jar;{0}\\libraries\\com\\paulscode\\codecwav\\20101023\\codecwav-20101023.jar;{0}\\libraries\\com\\paulscode\\libraryjavasound\\20101123\\libraryjavasound-20101123.jar;{0}\\libraries\\com\\paulscode\\librarylwjglopenal\\20100824\\librarylwjglopenal-20100824.jar;{0}\\libraries\\com\\paulscode\\soundsystem\\20120107\\soundsystem-20120107.jar;{0}\\libraries\\argo\\argo\\2.25_fixed\\argo-2.25_fixed.jar;{0}\\libraries\\org\\bouncycastle\\bcprov-jdk15on\\1.47\\bcprov-jdk15on-1.47.jar;{0}\\libraries\\com\\google\\guava\\guava\\14.0\\guava-14.0.jar;{0}\\libraries\\org\\apache\\commons\\commons-lang3\\3.1\\commons-lang3-3.1.jar;{0}\\libraries\\commons-io\\commons-io\\2.4\\commons-io-2.4.jar;{0}\\libraries\\net\\java\\jinput\\jinput\\2.0.5\\jinput-2.0.5.jar;{0}\\libraries\\net\\java\\jutils\\jutils\\1.0.0\\jutils-1.0.0.jar;{0}\\libraries\\com\\google\\code\\gson\\gson\\2.2.2\\gson-2.2.2.jar;{0}\\libraries\\org\\lwjgl\\lwjgl\\lwjgl\\2.9.0\\lwjgl-2.9.0.jar;{0}\\libraries\\org\\lwjgl\\lwjgl\\lwjgl_util\\2.9.0\\lwjgl_util-2.9.0.jar;{0}\\versions\\1.6.2\\1.6.2.jar net.minecraft.client.main.Main --username " + username + " " + token + " --version " + version + " --gameDir {0} --assetsDir {0}\\assets";
launch = String.Format(launch, directory);
string text = launch;
// WriteAllText creates a file, writes the specified string to the file,
// and then closes the file.
Directory.SetCurrentDirectory(#"C:\windows\system32\");
Process.Start("javaw.exe",
Path.Combine(launch));
What am I doing wrong?
Why do you need to call Path.Combine if your whole path is in one string?
Assuming your javaw.exe is actually in C:\windows\system32\, Process.Start("java.exe", launch); should work as intended.
Source - Path on MSDN: http://msdn.microsoft.com/en-us/library/system.io.path.aspx
Just use Java's real location instead of "javaw" like "c:\programfiles\Java\jre7\bin\javaw.exe"

Categories

Resources