Hello! (I'm new to StackOverflow)
I have a question concerning C# Console.
I want to open a .txt File which is in the same directory.
This is my Code (the name of the text file is README.txt):
string path = System.IO.Directory.GetCurrentDirectory() + #"\README.txt";
string[] FileContents = System.IO.File.ReadAllLines(path);
System.Diagnostics.Process.Start(path);
The code works fine, but I want the text file to be opened maximized. How do I make this?
By specifying the ProcessStartInfo parameters:
string notepadPath = Environment.SystemDirectory + "\\notepad.exe";
var startInfo = new ProcessStartInfo(notepadPath)
{
WindowStyle = ProcessWindowStyle.Maximized,
Arguments = "README.txt"
};
Process.Start(startInfo);
Related
I have a document which I am sending to print using below c# code
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
Verb = "print",
FileName = FileToPrintPath//put the correct path here
};
p.Start();
Now, I have a condition where instead of printing whole document I want to print juts from page number 2 to 5. How can I do this?
I don't know the direct answer to your question but you can easily figure this out with using the code below. Show a dialog and choose page number, number of copies etc. and see how it appears in printDialog1.PrinterSettings. once you know the format, remove the dialog code and hardcode it into Arguments:
using (PrintDialog printDialog1 = new PrintDialog())
{
if (printDialog1.ShowDialog() == DialogResult.OK)
{
var info = new ProcessStartInfo(**FILENAME**);
info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
// Use the debugger a message dialog to see
// contents of printDialog1.PrinterSettings
}
}
I wrote a quick test and here is what was stored in PrinterSettings:
[PrinterSettings Microsoft XPS Document Writer Copies=1 Collate=False Duplex=Simplex FromPage=0 LandscapeAngle=270 MaximumCopies=1 OutputPort=PORTPROMPT: ToPage=0]
So you need to pass FromPage and ToPage:
info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"" + "FromPage=2 ToPage=5";
In your code:
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
Verb = "print",
FileName = FileToPrintPath,//put the correct path here,
Arguments = "\"Printer Name Goes Here\" FromPage=2 ToPage=5";
};
Please not they are space separated arguments and if your printer name has spaces, you need to put the printer name within quotations.
I have an exe file which I run through windows command prompt and give command line arguments. I went through this post and ran the following command:
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
But all it did, is to give me resource files located in WindowsFormsApplication1\obj\Debug folder
I went through this post but it tells on how to execute the exe directly without the running it from cmd.
I even tried the following command:
string path = Path.Combine(Path.GetTempPath(), "MyApplication.exe");
It worked but after clearing my C:\Users\UserName\AppData\Local\Temp folder the application started giving an error.
I even tried the following command:
global::ApplicationName.Properties.Resources.MyApplication
but it gives byte[] and not the path to the application.
All I want to know is how to run the application which is embedded in my resources so that I can successfully execute the following command:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/K " + MyApplication+" Argument "+Path1+" "+Path2 ,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadToEnd();
using (System.IO.StreamWriter file = new System.IO.StreamWriter(resultFile))
{
file.WriteLine(line);
}
}
Extract the resource into a file in the filesystem and then run it.
byte[] fileContents = ApplicationName.Properties.Resources.MyApplication;
File.WriteAllBytes("MyApplication.exe", fileContents);
Now you can run the file using MyApplicaton.exe as path.
I used innosetup to install my application.
All the files for example are in program files\test
In the directory i have the exe file of my program and also ffmpeg.exe
Now in my code i did :
class Ffmpeg
{
NamedPipeServerStream p;
String pipename = "mytestpipe";
byte[] b;
System.Diagnostics.Process process;
string ffmpegFileName;
string workingDirectory;
public Ffmpeg()
{
workingDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath) + #"\workingDirectory";
ffmpegFileName = #"\ffmpeg.exe";
if (!Directory.Exists(workingDirectory))
{
Directory.CreateDirectory(workingDirectory);
}
ffmpegFileName = workingDirectory + ffmpegFileName;
Logger.Write("Ffmpeg Working Directory: " + ffmpegFileName);
}
public void Start(string pathFileName, int BitmapRate)
{
try
{
string outPath = pathFileName;
Logger.Write("Output Video File Directory: " + outPath);
Logger.Write("Frame Rate: " + BitmapRate.ToString());
p = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, PipeTransmissionMode.Byte);
b = new byte[1920 * 1080 * 3]; // some buffer for the r g and b of pixels of an image of size 720p
ProcessStartInfo psi = new ProcessStartInfo();
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.FileName = ffmpegFileName;
psi.WorkingDirectory = workingDirectory;
Thep roblem is that the directory workingDirectory not contain the ffmpeg.exe after installation . so if the user will run first time the program after installation the file will be missing .
I added the ffmpeg.exe to my project and set it to : Content and Copy always
What i want to do is that somehow to set the workingDirectory to the place where the user was installing the program if it's program file or any other directory .
Or to set the workigDirectory to the file ffmpeg.exe i already added to the project.
The problem is after installation the user will run the program and the directory workingDirectory will be empty .
if the file ffmpeg.exe is installed in the same directory where the assembly that calls it resides, then your could write:
string fullPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string installDirectory = Path.GetDirectoryName( fullPath );
However if you really want to copy that file from the installed directory to the Application.LocalUserAppDataPath
// Assuming that the LocalUserAppDataPath has already been created
string destDirectory = Path.Combine(Application.LocalUserAppDataPath, "workingDirectory");
File.Copy(Path.Combine(installDirectory, "ffmpeg.exe"),
Path.Combine(destDirectory, "ffmpeg.exe"), true);
but then, why you don't search the functionality of InnoSetup to discover how to place the ffmpeg.exe file in the workingDirectory during setup? That will solve all your issues here.
1- You should create a registry key that can store the installation path and path of any other folder that your application needs. Check this question on how to do that: How to write install path to registry after install is complete with Inno setup
2- Read the registry settings at application startup. Use those paths.
You can use Application.StartupPath to reference the folder path where are your main executable and ffmpeg.exe
Application.StartupPath
Gets the path for the executable file that started the application,
not including the executable name.
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx
And then if you need it... copy the ffmpeg.exe to the working directory that you choose.. though I think its not a good idea...
File.Copy(Source, Target)
http://msdn.microsoft.com/en-us/library/c6cfw35a.aspx
string destDirectory = Path.Combine(Application.LocalUserAppDataPath, "workingDirectory");
string ffmpegDestPath = Path.Combine(destDirectory, "ffmpeg.exe");
string ffmpegSrcPath = Path.Combine(Application.StartupPath, "ffmpeg.exe");
if (!File.Exist(ffmpegDestPath)) {
if (!Directory.Exists(destDirectory)) Directory.Create(destDirectory);
File.Copy(ffmpegSrcPath , ffmpegDestPath );
}
I'm trying to start application "GA.exe", but on start it's taking data from file "acc.txt".
If I start it normallly (via double click :-)) it works, but if I use code below it say "Can't find acc.txt".
My first idea:
Process.Start(pathToGA.exe);
Second idea:
ProcessStartInfo pinfo = new ProcessStartInfo()
{
Arguments = FolderWithGA.exePath,
FileName = pathToGA.exe,
};
And both don't work.
You should set ProcessStartInfo.WorkingDirectory to the directory that holds acc.txt and GA.exe:
ProcessStartInfo pinfo = new ProcessStartInfo()
{
Arguments = FolderWithGA.exePath,
FileName = pathToGA.exe,
WorkingDirectory = FolderWithGA
};
What I Have
I am currently writing a program which takes a specified file and the performs some action with it. Currently it opens it, and/or attaches it to an email and mails it to specified addresses.
The file can either be of the formats: Excel, Excel Report, Word, or PDF.
What I am currently doing is spawning a process with the path of the file and then starting the process; however I also am trying to fix a bug feature that I added which adds the verb 'PrintTo' to the startup information, depending on a specified setting.
What I Need
The task I am trying to accomplish is that I would like to have the document open and then print itself to a specified printer named within the program itself. Following that up, the file should then close itself automatically.
If there is no way to do this generically, we might be able to come up with a way to do it for each separate file type.
What you Need
Here is the code I'm using:
ProcessStartInfo pStartInfo = new ProcessStartInfo();
pStartInfo.FileName = FilePath;
// Determine wether to just open or print
if (Print)
{
if (PrinterName != null)
{
// TODO: Add default printer.
}
pStartInfo.Verb = "PrintTo";
}
// Open the report file unless only set to be emailed.
if ((!Email && !Print) || Print)
{
Process p = Process.Start(pStartInfo);
}
How I'm doing...
Still stumped... might call it like Microsoft does,'That was by design'.
The following works for me (tested with *.doc and *.docx files)
the windows printto dialog appears by using the "System.Windows.Forms.PrintDialog" and for the "System.Diagnostics.ProcessStartInfo" I just take the selected printer :)
just replace the FILENAME with the FullName (Path+Name) of your Office file. I think this will also work with other files...
// Send it to the selected printer
using (PrintDialog printDialog1 = new PrintDialog())
{
if (printDialog1.ShowDialog() == DialogResult.OK)
{
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(**FILENAME**);
info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
info.Verb = "PrintTo";
System.Diagnostics.Process.Start(info);
}
}
Theoretically, according to an article on MSDN you should be able to change it to be along the lines of (untested):
// Determine wether to just open or print
if (Print)
{
if (PrinterName != null)
{
pStartInfo.Arguments = "\"" + PrinterName + "\"";
}
pStartInfo.CreateNoWindow = true;
pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pStartInfo.UseShellExecute = true;
pStartInfo.WorkingDirectory = sDocPath;
pStartInfo.Verb = "PrintTo";
}
get from Rowland Shaw:
ProcessStartInfo startInfo = new ProcessStartInfo(Url)
{
Verb = "PrintTo",
FileName = FilePath,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
Arguments = "\"" + PrinterName+ "\"",
};
Process.Start(startInfo);
FilePath look like 'D:\EECSystem\AttachedFilesUS\53976793.pdf'
PrinterName is your printer name
copy the code,it will work.