I want to open a text file programmatically using C#. I have used :
System.Diagnostics.Process.Start(test.txt);
but this code is causing OS command injection problem when scanning for threats.
Is there any way that i can open a text file programmatically?? or way to bypass that OS command injection?
Thank you
You should call a program, say notepad:
Process.Start("notepad.exe", fileName);
the argument is the file name:
Process.Start("notepad.exe", "Test.txt");
See the problem with your code in the comments of this post:
Open a file with Notepad in C#
Try:
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";
string _path = "c:/filepath";
startInfo.Arguments = string.Format("/C start {0}", _path);
process.StartInfo = startInfo;
process.Start();
Related
So I am witting a java program for interacting with filesystem.
Java program itself cannot run as super user. I created a special user for this program and gave it some special privileges to run some commands without password (via visudo file).
So I I'd like to check if a file exist. I used:
if(f.exists() && !f.isDirectory()) but the problem is this fails if I am checking if files exist that is read/write protected or if it belongs to another user.
That is why I need to use bash. for example, when I am retrieving information about file I use the following:
String[] command = new String[] { "sudo", "stat", filepath, "-c", "%F##%s##%U##%G##%X##%Y##%Z" };
process = runtime.exec(command); and then just parse the output.
For example moving a file I use this:
String[] command = new String[] {
"sudo",
"-u",
user,
"mv",
source,
target
};
So now I am looking for a way to get simple true/false response when checking if file exist.
I think I could use find command or something similar?
I've solved my issue by using the following command:
String[] command = new String[] { "sudo", "-u", user, "test", "-f", path };
Where user is user who I am running command as. And path is the path-to-file we are testing.
I then read the execute and read command's output in the code below.
I hope it helps someone someday...
process = runtime.exec(command);
errbr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((messageLine = errbr.readLine()) != null) {
message += messageLine + ";";
}
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((messageLine = br.readLine()) != null) {
message += messageLine;
}
I am new to Coded UI. I have written a simple code to execute a .bat file from a CodedUITestMethod1() as below:
thisProcess.StartInfo.CreateNoWindow = true;
thisProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
thisProcess.StartInfo.FileName = #"C:\BVTBatch\PlayBack.bat";
thisProcess.StartInfo.UseShellExecute = false;
thisProcess.StartInfo.RedirectStandardOutput = true;
thisProcess.Start();
thisProcess.WaitForExit();
strException = thisProcess.StandardOutput.ReadToEnd();
Problem statement: When I debug the script, it gets executed but the batch file does not run. I tried executing iexplorer.exe, and observed same issue. The script gets executed with pass, but IE browser does not start.
However if I execute the same code from other console application or Unit Test project method, it gets executed successfully.
Can someone suggest what is the reason behind this? and how can we fix this in CodedUI?
Thanks in advance.
This would seem legit:
thisProcess.StartInfo.FileName = ("C:\BVTBatch\PlayBack.bat");
Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo = new ProcessStartInfo ("C:\BVTBatch\PlayBack.bat");
p.Start();
I almost migrated all tests to Robolectric 3.0. It was huge deal :)
I have last failing test because I can not access test database that I used for migration tests. Next code produces NPE:
String filePath = getClass().getResource( "/test.db" ).toURI().getPath();
Do you know a way how to get absolute path to this file in Robolectric 3.0? I want to avoid hardcoding since I want to have test working on all machines
Maybe will be helpful for someone. My code:
private void copyTestDatabase( String resourceDBName )
throws URISyntaxException, IOException
{
String filePath = RuntimeEnvironment.application.getPackageResourcePath() + "/src/test/res" + resourceDBName;
String destinationPath = RuntimeEnvironment.application.getDatabasePath( "<my-db-name>.db" ).getAbsolutePath();
File to = new File( destinationPath );
to.mkdirs();
to.delete();
Files.copy( new File( filePath ), to );
}
Let me know if you have more elegant solution. Post the answer I will accept it
This is just a quick question that I am sure someone will be able to answer quickly as I am most likely just missing something.
Lets say I have the following directory layout
Folder1
-> CurrentlyRunning.EXE
-> Folder2
ProcessToStart.Bat
ApplicationToStartFromBat.exe
This is the code inside the applications.
CurrentlyRunning.EXE:
var proc = new Process
{
StartInfo =
{
FileName = "Folder2/ProcessToStart.Bat",
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false
}
};
proc.Start();
ProcessToStart.Bat:
START ApplicationToStartFromBat.exe
Now, if I run ProcessToStart.Bat by double clicking on it, it will open ApplicationToStartFromBat.exe with no problems (which is good). If I run CurrentlyRunning.EXE (which will execute the code I posted above) the BAT file fails saying it can't find my EXE (which is really weird).
If I change the BAT file to:
START Folder2/ApplicationToStartFromBat.exe
and then run CurrentlyRunning.EXE, the bat will then properly open ApplicationToStartFromBat.exe. My problem is I can not change the code inside the bat for one reason or another.
Why is proc.Start() causing the bat file search root directory to change, and how do I stop this from happening?
Thanks
I think it is to do with where the working directory is for your exe file.
Try using ProcessStartInfo.WorkingDirectory to set the correct directory for your batch file.
var proc = new Process
{
StartInfo =
{
FileName = "Folder2/ProcessToStart.Bat",
WorkingDirectory = "DirectoryPath";
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false
}
};
proc.Start();
I'd like to do something very simple: Create/write to a file located in the remote workspace of a slave via the jenkins groovy post-build script plug-in
def props_file = new File(manager.build.workspace.getRemote() + "/temp/module.properties")
def build_num = manager.build.buildVariables.get("MODULE_BUILD_NUMBER").toInteger()
def build_props = new Properties()
build_props["build.number"] = build_num
props_file.withOutputStream { p ->
build_props.store(p, null)
}
The last line fails, as the file doesn't exist. I'm thinking it has something to do with the output stream pointing to the master executor, rather than the remote workspace, but I'm not sure:
Groovy script failed:
java.io.FileNotFoundException: /views/build_view/temp/module.properties (No such file or directory)
Am I not writing to the file correctly?
While writing onto slave you need to check the channel first and then you can successfully create a file handle and start reading or writing to that file:
if(manager.build.workspace.isRemote())
{
channel = manager.build.workspace.channel;
}
fp = new hudson.FilePath(channel, manager.build.workspace.toString() + "\\test.properties")
if(fp != null)
{
String str = "test";
fp.write(str, null); //writing to file
versionString = fp.readToString(); //reading from file
}
hope this helps!
Search for words The post build plugin runs on the manager and doing it as you say will fail if you are working with slaves! on the plugin page (the link to which you've provided) and see if the workaround there helps.
Does the folder /views/build_view/temp exist?
If not, you will need to do new File( "${manager.build.workspace.remote}/temp" ).mkdirs()