I am trying to write a program to export the System Info using the Msinfo32 utility on a button click. I am doing this in the C# using the Powershell class. Now, the thing is that the compiled application is already set to run with Administrator Privileges. But, I am still getting the Access Denied Error when the utility starts saving to Desktop. Below is the Source Code :-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management.Automation;
using System.IO;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
namespace Diagnostic_Tool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 10;
Runspace Run = RunspaceFactory.CreateRunspace();
Run.Open();
progressBar1.Value = 30;
Pipeline pipeline = Run.CreatePipeline();
progressBar1.Value = 50;
Command Msinfo32 = new Command("Msinfo32.exe");
string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Msinfo32.Parameters.Add("/nfo");
Msinfo32.Parameters.Add(path);
progressBar1.Value = 70;
pipeline.Commands.Add(Msinfo32);
pipeline.Invoke();
pipeline.Stop();
Run.Close();
progressBar1.Value = 100;
MessageBox.Show("The Task Has Completed Successfully");
}
}
}
Could anyone please tell what is happening wrong?
You're getting access denied because you're telling msinfo to write data directly to the Desktop directory itself, and not to a file.
Your 'path' variable contains the name of the Desktop directory. You need to append a filename to this parameter. eg:
OLD: MSinfo32.Parameters.add(path)
NEW: MSinfo32.Parameters.add(path + "\\foo.txt")
Related
I'm trying to connect to oracle database but I get this error
System.BadImageFormatException: 'Could not load file or assembly 'Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format.'
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
string ordb = "Data Source =orcl ;User Id = hr ; password =hr;";
OracleConnection conn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
conn = new OracleConnection(ordb);
conn.Open();
}
}
}
Check your dll references, your app seems to be an X64 base, but maybe one of the dll's is of a different format
This question already has an answer here:
C# process.Start filename and passing arguments
(1 answer)
Closed 5 years ago.
I have an R Script that I am executing through the following command in CMD:
Rscript.exe C:\Users\Stefan\Documents\r_directory\script.R 10 arg2
What I want to do now is to build a windows form with C# with a couple of text boxes that will serve as user input for the arguments in the CMD command.
I've attemted to run the command with the following code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//this is to run program from batch System.Diagnostics.Process.Start("c:\\file2.bat");
string strCmdText;
strCmdText = "Rscript.exe C:\\Users\\Stefan\\Documents\r_directory\\script.R 10 arg2";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
}
}
The cmd windows prompts and all I'm geting is:
C:\Users\Stefan\source\repos\WindowsFormsApp2\WindowsFormsApp2\bin\Debug>
The R script is not executed. Any clues why?
This is probably a very basic question, but I'm just starting to learn C#.
Many thanks.
Use a class called Process from its own C#
Process proc = new Process
{
StartInfo =
{
FileName = Rscript.exe,
Arguments = strCmdText,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
proc.Start();
All I want to do is to retrieve data from tables in .accdb file.
Here is my full application:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Microland.accdb;Persist Security Info=False;");
myConn.Open();
OleDbCommand myQuery = new OleDbCommand("select CustID from Customers WHERE CustID = 1;", myConn);
OleDbDataReader myReader = myQuery.ExecuteReader();
if(myReader.HasRows)
{
myReader.Read();
label1.Text = myReader.ToString();
}
myConn.Close();
}
}
}
I think I am missing some using at the very top or my code is somewhat broken becasue then I click the button1 the label1 text changes to System.Data.OleDb.OleDbDataReader.
Also is this the correct way to connect to access data base (.accdb) Do I need to do this walkthrough in order for it to work or this is irrelevant to what I need to do?
Thanks for any information! Very appreciated
When you call myReader.ToString() it returns a string that represents the current object. So "System.Data.OleDb.OleDbDataReader" is exactly that.
It seems like you want the label to be equal to the data that's read. I'm not familiar with this reader in particular, but refer here for documentation.
You'll need to call one of the Get*() functions.
label1.Text = myReader["CustID"] as string; // if nullable field
Or
label1.Text = (string)myReader["CustID"] // if not null
Hi everyone I've been creating this program for a day now. But it seems that I can't get it to work. What I want is, I have a windows form, who will get the IP address input and will execute a .bat file.
#echo off
wmic /node:x computersystem get username
where x is the string variable for my IP.
the command will print out the current logged in username.
Here's my c# code:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace wmic_forms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string x;
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "C:\\ken.bat";
Process p = Process.Start(psi);
string strOutput = p.StandardOutput.ReadToEnd();
//p.WaitForExit();
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.Arguments = textBox1.Text;
MessageBox.Show(strOutput);
//Console.WriteLine(strOutput);
//Console.ReadLine();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//my input text box for IP
x = textBox1.Text;
}
}
}
The code runs with no error. But it would only print out the entire strings from .bat file. Please give me feedback on how can I pass variable x to .bat file and execute its command.
You don't need a batch file to do that, simply run wmic with those parameters. And you especially don't need to follow malware practices of storing files in system folders, like the C: drive.
As to the specific problem in your code, you're setting the process object's properties after you launch it instead of before. Of course they don't matter anymore.
And while we're on the topic of refactoring, I'll remind you that it's not 1985 anymore to launch chains of scripts because the OS doesn't expose its inner workings. You can easily access the WMI API directly from C# using managed classes: https://msdn.microsoft.com/en-us/library/system.management.managementscope(v=vs.110).aspx
I have run into a problem where my C# code is creating a whole new database instead of using a preexisting one. Then my program runs into errors where the program cannot find the table to insert the information even though the preexisting database has the table because the code itself is looking at the new table. Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Finisar.SQLite;
namespace WestSlope
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
//SQLiteDataAdapter sqlite_datareader;
sqlite_conn = new SQLiteConnection("DataSource=ClientLogDB.db;Version=3;New=True;Compress=True;");
//open conection
sqlite_conn.Open();
//create sql commands
sqlite_cmd = sqlite_conn.CreateCommand();
//Let SQLite command know query is known
sqlite_cmd.CommandText = "INSERT INTO ASAM (ASAMone, ASAMtwo, ASAMthree, ASAMfour, ASAMLim, ASAMLimEX) VALUES ('Had to call', 'Reffered', 'Had to call', 'Watched', 1 , 'Injured legs');"
;
//execute query
sqlite_cmd.ExecuteNonQuery();
sqlite_conn.Close();
}
}
}
What the code is supposed to do is when the user presses a button the program will save information to the preexisting database; but, as you can see the program is making a new database instead of using the preexisting one.
Use new=false in your connection string to use existing database file.
Following should be the connection string:
sqlite_conn = new SQLiteConnection("DataSource=ClientLogDB.db;Version=3;New=False;Compress=True;");