How to pass arguments to an HtmlFile from C#?
Like: System.Diagnostics.Process.Start("Sample.html","Arguments");
If I execute the above code the "Sample.html" file should be opened and it should do something with the "arguments".
Process.Start(
#"C:\Program Files\Internet Explorer\iexplore.exe",
"file:///c:/path/to/file/Sample.html?param1=value1"
);
UPDATE:
To figure out the default browser location:
class Program
{
[DllImport("shell32.dll")]
public extern static int FindExecutable(
string forFile,
string directory,
StringBuilder result
);
static void Main(string[] args)
{
var browserLocation = new StringBuilder(1024);
// make sure you specify the correct path and the file actually exists
// or the FindExecutable will return an empty string.
FindExecutable(#"d:\work\html\index.htm", null, browserLocation);
Process.Start(
browserLocation.ToString(),
"file:///d:/work/html/index.htm?param1=value1"
);
}
}
Related
Edit: I noticed this doesn't happen when I just do: File.WriteAllText(#"C:\Users\(usr)\Documents\Test\Data\test.txt", "0"); without Globals.dmp so evidently it must be something with Path.Combine() I tried various variations of the composition but with same results.
I want to create a folder and a text file in my c# program made with Visual Studio. What I'm doing is having a check at Form Load to see if the file exists, and if it does not, create the file. I'm doing it like this:
if(!File.Exists(Globals.dmp))
{
File.Create(Globals.dmp);
File.WriteAllText(Globals.dmp, "0");
}
The composition of globals.dmp is the following:
public static string dmp = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Constants.dmp);
While Constants.dmp is like this:
public static string dmp = #"Test\Data\test.txt
This should work, but when I try to run it, Visual studio reports: System.UnauthorizedAccessException: 'Access to the path 'C:\Users\(usr)\Documents\Test\Data\test.txt' is denied.'The line that fails is: File.Create(Globals.dmp);I tried putting File.SetAttributes(Globals.dmp, new FileInfo(Globals.dmp).Attributes | FileAttributes.Normal); above File.Create but the same thing happens. Any insight is greatly appreciated.
Run your visual studio as administrator and see it works or not.
I solved the problem by adapting PathCombineAndCanonicalize1() from the second solution in this question
What I did is:
public static string PathCombineAndCanonicalize1(string path1, string path2)
{
string combined = Path.Combine(path1, path2);
return combined;
}
public partial class ProgramDirectories
{
public static string directory = #"DillData\";
public static string documentspath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
public static string pathforfiles = PathCombineAndCanonicalize1(documentspath, directory);
public static string sps = PathCombineAndCanonicalize1(pathforfiles, Constants.sps);
}
With Constants.sps="text.txt";
And then I could easily do:
if (!Directory.Exists(ProgramDirectories.pathforfiles))
{
Directory.CreateDirectory(ProgramDirectories.pathforfiles);
}
if(!File.Exists(ProgramDirectories.sps)
{
File.WriteAllText(ProgramDirectories.sps, "0");
}
I created the function pro:
it contains the process array
it calls another write function to make the file and write into it.
the write function writeproc:
it checks if the file at specified path is present or not.
if not it generates the file else it appends the text into the file.
when i run the code it is not doing anything.... :(
This is the main method for the console app that i have made in c#.
[STAThread]
static void Main(String[] args)
{
pro();
}
pro function:
static void pro()
{
Process[] localAll = Process.GetProcesses();
String path_pro = "C://KEYLOGS//processes.txt";
foreach(Process proc in localAll)
{
writeproc(path_pro, proc);
}
}
writeproc function:
static void writeproc(String p, Process the_process)
{
if (!File.Exists(p))
{
using (StreamWriter sw = File.CreateText(p))
{
//empty file generated.
}
}
else
{
using (StreamWriter sw = File.AppendText(p))
{
sw.WriteLine("Process: "+the_process);
}
}
}
This may be the cause of two different things.
1: The folder does not exist on your C drive so the file can't be created. (It will throw a System.IO.DirectoryNotFoundException)
Add Directory.CreateDirectory(p); to the start of your writeproc method.
2: You don't have enough rights to write to your C drive. (It will throw a System.UnauthorizedAccessException)
I suggest adding a breakpoint in your writeproc method to see what exception is being thrown.
I have a large directory of folders and files that contain a space at the end of the name, I'm trying to rename the directories with that space to one without, so that another application would be able to access it.
I'm using C# (but if there's a better option that would fix that issue please suggest) and here's my entire code:
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace removing_spaces_in_directories_names
{
class Program
{
public static string path = "../../../old_directory";
static void Main(string[] args)
{
DirectoryInfo di = new DirectoryInfo(path);
WalkDirectoryTree(di);
Console.ReadLine();
}
static void WalkDirectoryTree(System.IO.DirectoryInfo root)
{
if (root.Name != "old_directory")
{ renameDirectory(root); }
DirectoryInfo[] diArr = root.GetDirectories();
foreach(DirectoryInfo di in diArr)
{
WalkDirectoryTree(di);
}
}
static void renameDirectory(System.IO.DirectoryInfo dir)
{
Console.WriteLine("renaming: " + dir.FullName);
string newName = ReplaceLastOccurrence(dir.FullName, " ", "");
if (Directory.Exists(dir.FullName) == false)
{
//dir.MoveTo(newName);
String oldName = #"\\?\"+dir.FullName;
Directory.Move(oldName,newName);
}
}
public static string ReplaceLastOccurrence(string Source, string Find, string Replace)
{
int place = Source.LastIndexOf(Find);
if (place == -1)
return Source;
string result = Source.Remove(place, Find.Length).Insert(place, Replace);
return result;
}
}
}
I have tried adding "\?\" to the beginning of the folder name as suggested here but that's not working, the error I'd get if I add it is: Illeagal characters in path.
On the other hand if I use dir.MoveTo(newName); without the "\?\" characters I'd get the error: Could not find a part of the path 'Volunteer Information '
How can I go through this if at all? would perhaps running this application on linux rather than windows help?
For each directory that you want to rename (remove the trailing space at the end in this case), let's say your DirectoryInfo variable is called di
You want to do this:
string oldName = di.FullName;
string newName = oldName.TrimEnd();
Directory.Move(oldName, newName);
I rewrote this in a PHP application that's sitting on linux and it worked.
I've learned a lot over the past few months mainly on this site.
I'm getting into a little hole here with this one though.
I'm trying to write or change the values in a specific INI file.
I'm using a custom class made by a StackOverflow Guru by the name of "Danny Beckett" that I really like and understand. Been using this for a while now with no issue. Works perfectly for most anything while writing and reading INI files... Until now.
This is where he talks about it.
Reading/writing an INI file
This is the Class I'm using to read and write the INI's.
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
// Change this to match your program's normal namespace
namespace MyProg
{
class IniFile // revision 10
{
string Path;
string EXE = Assembly.GetExecutingAssembly().GetName().Name;
[DllImport("kernel32")]
static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);
[DllImport("kernel32")]
static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);
public IniFile(string IniPath = null)
{
Path = new FileInfo(IniPath ?? EXE + ".ini").FullName.ToString();
}
public string Read(string Key, string Section = null)
{
var RetVal = new StringBuilder(255);
GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
return RetVal.ToString();
}
public void Write(string Key, string Value, string Section = null)
{
WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
}
public void DeleteKey(string Key, string Section = null)
{
Write(Key, null, Section ?? EXE);
}
public void DeleteSection(string Section = null)
{
Write(null, null, Section ?? EXE);
}
public bool KeyExists(string Key, string Section = null)
{
return Read(Key, Section).Length > 0;
}
}
}
This is the code I'm using to actually make the changes....
var MyIni = new IniFile("Settings");
var SelectedPdata = MyIni.Read("SelectedPdata");
var SelectedSQL = MyIni.Read("SelectedSQL");
var SetupIniLocation = SelectedPdata + "\\Setup\\Sidexis\\Setup.ini";
var SetupIni = new IniFile(SetupIniLocation)
SetupIni.DeleteKey("VZPDATA", "CLIENT");
SetupIni.Write("VZPDATA", SelectedPdata, "CLIENT");
SetupIni.DeleteKey("COMBOSQL", "CLIENT");
SetupIni.Write("COMBOSQL", SelectedSQL, "CLIENT");
What is does:
It reads two Parameters from my settings INI file and passes them to variables. Then uses those variables to write them into the setup.ini file.
The target "Setup.ini"
[CLIENT]
Aufruf="msiexec.exe /i Sidexis.MSI VZSIDEXIS="C:\Sidexis\" SQLDIALOGTYPE=1 LANGUAGE=1033 PROPDSN="PDATA_SQLEXPRESS" PROPDBO="" INSTALLATIONTYPE=FULL SERIALNUMBER=6000000837 VZPDATA="\\JONMER-WIN7X64\PDATA\" COMBOSQL="JONMER-WIN7X64\PDATA_SQLEXPRESS" COMPANYNAME="" NEEDEDDISKSPACE=104857600 MASTERCLIENT=TRUE /QB!"
The paramaters that I need to modify are: COMBOSQL and VZPDATA.
It is able to modify it... But it just puts them at the bottom.
I'm sure it has something to do with the formatting because if I format it like a regular INI file. (each parameter on its own line) It works fine....
Is there a way to pass a string argument to a process which is spawned from my own process.
I have in my main application:
Process.Start(Path.Combine(Application.StartupPath, "wow.exe");
wow.exe is another app I created. I need to pass argument to this exe (a string). How can I achieve this typically?
What I tried:
ProcessStartInfo i = new //........
i.Argument = "cool string";
i. FileName = Path.Combine(Application.StartupPath, "wow.exe");
Process.Start(i);
And in the main of wow application i wrote:
static void Main()
{
//print Process.GetCurrentProcess().StartInfo.Argument;
}
But I never get my string there in second application's Main. Here is a question which asks why, but no how to solve it..
Edit: Environment.GetCommandLineArgs()[1], it has to be. Nevertheless, got it working. Accepted #Bali's answer as he cameup first with this answer. Thanks all
To get the arguments passed you can either use the string[] args in your Main, or you can use Environment.GetCommandLineArgs.
Example:
Console.WriteLine(args[0]);
or
Console.WriteLine(Environment.GetCommandLineArgs[0]);
You probably want a
static void Main(string[] args)
{
}
where args contains the arguments you passed in
Here's an example how you can get arguments passed to your exe:
static void Main()
{
string[] args = Environment.GetCommandLineArgs();
string firstArgument = args[0];
string secondArgument = args[1];
}
or change your main method a bit:
static void Main(string []args)
{}
In your wow.exe program.cs
static void Main()
{
//Three Lines of code
}
change it to
static void Main(string[] args)
{
//Three Lines of code
}
string[] args. will now contain your arguments passed to your exe.
Or you can use
string[] arguments = Environment.GetCommandLineArgs();
Your arguments are broken by space " ".