I need to acces a file (excel, txt) from onedrive - c#

So until now I was testing the application using a local path to find the file that I try to work with
string path = #"A:\Log\OLP_Application_Storage\call.bat";
try
{
Process process = new Process();
process.StartInfo.FileName = "call.bat";
process.StartInfo.WorkingDirectory = #"A:\Log\OLP_Application_Storage\call.bat";
process.StartInfo.Arguments = "call.bat";
process.Start();
}
I have a bat file that calls a python script, with this path is working like a charm. The thing is that I try my app to be used by multiple users (the ones that have acces to my onedrive) so I need to use the share link from onedrive, I dont know how to addapt it, only replacing the path with the link it does not work.
string path = https://companyname-my.sharepoint.com/:x:/p/userid/ewaaiwjfafjwiajawfiaf";
try
{
//string pth;
//pth = "A:\Log\OLP_Application_Storage\call.bat";
Process process = new Process();
process.StartInfo.FileName = "call.bat";
process.StartInfo.WorkingDirectory = "https://companyname-my.sharepoint.com/:x:/p/userid/ewaaiwjfafjwiajawfiaf";
process.StartInfo.Arguments = "call.bat";
process.Start();
}
I tried something like this but is not working.
Thank you for your time!
Edit: I am not very good at exprimation so it may be confusing.
I use a txt file as a list of ids of users, I use an excel file to store some data when a user uses the app, he insert in couple of spaces, he press send and the app saves it inside excel file. I did all the code on local, but now I want to share the app inside the company. To be able to write mutiple users at the same time inside the excel file, it has to be inside Onedrive. So now I have to find out how to get to it for me to use it (The difference should be seen in the 2 codes example). Thank again for the wasted time! It may be a easy problem but I am new to the app production world...

Related

How to print a docx file in reverse order in C#

I'm coding a Wpf application to print docx files. I can normally print it. This my code:
var info = new Process();
info.StartInfo = new ProcessStartInfo(filePath) //in this pass the file path
{
UseShellExecute = true
};
info.StartInfo.Verb = "Print";
info.StartInfo.CreateNoWindow = true;
info.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
info.Start();
info.WaitForExit();
info.Dispose();
But now, I want to print pages in this docx file by reverse order. First I wonder how can I use AddCoreProperty in DOCX package, but i don't find anyway to use it to print reverse order. I searched but could find any helpful information. Maybe by another solution, all useful with me.

Process.Start from .txt file to textbox

I'm trying to solve a problem i got. My job is to make little app, that will show text which is inside of .txt file in the app window, but for some reason they told me that i have to use # ShellExecute(use Process.Start).
Is there even a way to do it? Because when i use ShellExecute, that file opens in notepad after button press, which is, I guess, point of using Shell.
There is little code of what i tried to do, but without success.
Thanks in advice!
string filePath = #"C:\Folder\file.txt";
ProcessStartInfo psi = new ProcessStartInfo(filePath);
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
var proc = Process.Start(psi);
string s = proc.StandardOutput.ReadToEnd();
textBox1.Text = s;
Instead of using ProcessStartInfo, try StreamReader like this :
string filePath = #"C:\Folder\file.txt";
StreamReader sr = new StreamReader(filePath);
string s = sr.ReadToEndAsync().GetAwaiter().GetResult();
Console.WriteLine(s);
Use Async method to read all text without blocking.
If you absolutely need to do that, you can create a second application TxtToTextBox, which you can run from your first application using Process.Start (initialize ProcessStartInfo with the path to that application instead of the txt file).
Then you can give that process an argument pointing to the file using psi.Arguments = $"\"{filePath}\"; (this also adds quotation marks around your path, so spaces are escaped).
Then in your second application you can do the sane thing, and simply read the file with File.ReadAllLines(args[0]) and print that into your text box.
If possible, I would recommend talking to whoever told you to use Process.Start and asking them for more reasons as to why you should use is, as this is one of the most roundabout ways to do this I could think of.

Download File by starting a Chrome Process

I want to download a file by just starting a new Process of Chrome. I've found the parameter "--download", so the solution should be "CHROMEPATH --download URI". ( http://peter.sh/experiments/chromium-command-line-switches/#download )
I wrote the code to start the process in C#, and yes, I know there are other options to do this like webclient, but I don't want to implement the download per se in my code.
string FILEURI = "example.org/file.png";
System.Diagnostics.Process prozess = new System.Diagnostics.Process();
prozess.StartInfo.FileName = #"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
prozess.StartInfo.Arguments = "--download " + FILEURI;
prozess.Start();
This works without any problem, but just opening the link "file://FILEURI". So I can't download it without any user interaction.
Chrome always downloads to the download folder. You could try to just get the latest download file, like this:
new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"Downloads"))
.GetFiles()
.OrderByDescending(f => f.CreationTime)
.First()
This has risks (as outlined by DavidG) as well as the fact the user might download another file, etc. So this is not necessarily the best way.

Print multiple images using Process.StartInfo.Verb=“print” command

I am using this code for print with windows print pictures...
string fileName = #"C:\Images\12.jpg";
var p = new Process();
p.StartInfo.FileName = fileName;
p.StartInfo.Verb = "Print";
p.Start();
I want to open multiple images from directory into this, how can i do it?
I tried this code, but does not work:
var p = new Process();
DirectoryInfo d = new DirectoryInfo(#"Directory address");
FileInfo[] Files = d.GetFiles("*.jpg");
foreach (FileInfo file in Files)
{
p.StartInfo.FileName += file.FullName.ToList();
p.StartInfo.Verb = "Print";
p.Start();
}
From your code example, it appears you want to simply invoke separate "print" verb commands for each file. If so, then you should be able to accomplish that by simply assigning the file name in your loop, instead of enumerating the characters of the file name and appending that to the FileName property:
DirectoryInfo d = new DirectoryInfo(#"Directory address");
FileInfo[] Files = d.GetFiles("*.jpg");
ProcessStartInfo psi = new ProcessStartInfo();
psi.Verb = "Print";
foreach (FileInfo file in Files)
{
psi.FileName = file.FullName;
Process.Start(psi);
}
Note that you can't reuse a single Process object for this purpose. Once a given Process object has been started, it can't be started again. But you can reuse a ProcessStartInfo object, starting a new process with each iteration of the loop.
EDIT:
From your comment:
I do not want to simply invoke separate "print" verb commands for each file...I want to add all files in one "print" verb command
This is not possible using the Process class. By definition, the DDE "print" verb (i.e. "command") handles only a single document. If you are willing to do a lot of extra work, you can write your own DDE client that attempts to use DDEEXEC to iteratively interact with a DDE server that knows how to print your image files. But a) this is a lot more work, and b) it still will only work if you happen to have a program installed that handles printing of image files via DDEEXEC (the Windows built-in support for printing images does not).
I recommend you stick with the above.
(And for future reference, if you only want to call Process.Start() once, putting it inside a loop is definitely not the way to go. Your question would have been more clear if your example code bore any resemblance at all to what you were actually trying to do. :) )

How to print various file types programmatically

I am writing an app which performs some tests and produces a number of different reports. These may be any combination of Labels, PDF for end customer, PDF for repair department, XML file etc.
Depending on the report type, I need to send the file to either the file system or to one of a number of different printers (A4, label etc). Ideally there should be no popup windows - just straight to paper.
How can I send a file (PDF, XML) to a printer? I had thought that for XML/Text I could just File.Copy to LPTn but that doesn't seem to work. For PDF I'm guessing that I could call Acrobat with some parameters which cause the PDF to be printed.
The printers I use are mapped to LPTn. Is there a better way to do this and to store the definitions in the app? i.e. Labels go to MyLabelPrinter and A4 PDFs got to MyA4Printer.
Has anyone done this?
ProcessStartInfo info = new ProcessStartInfo("[path to your file]");
info.Verb = "PrintTo";
info.Arguments = "\"[printer name]\"";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
Take a look at this webpage. You should find the info you are looking at for PDF.
For example, it will look like that:
ProcessStartInfo infoOnProcess = new ProcessStartInfo("C:/example.pdf");
info.Verb = "PrintTo";
//Put a if there, if you want to change printer depending to file extension
info.Arguments = "\"HP-example-Printer\"";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(infoOnProcess);

Categories