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.
Related
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...
I have a winforms application in C# where I have to open a certain Folder.
I use
System.Diagnostics.Process.Start(pathToFolder);
This results in the following exception:
System.ComponentModel.Win32Exception (0x80004005): Access is denied
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo
startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at MyApp.openLogFolderToolStripMenuItem_Click(Object sender, EventArgs e)
I have already checked the following things:
The folder exists
The user has rights to the folder (can open it in Explorer)
Another thing is that if I use Process.Start() to open a file inside this folder, it works.
Can anyone give me a hint?Cheers
Edit
My goal is to open the folder in Explorer.
The pathToFolder is something like H:\Something\App.Name\Log
According to Microsoft Doc's the System.Diagnostics.Process.Start(string) runs the file or process (and therefore does not open the folder). For opening a folder, this doc page sugests that you might do this with System.Diagnostics.Process.Start(string, string) where first should be a way to explorer, Total commander or something similar, and second should be a argument telling the used explorer what to do (open the folder pathToFolder).
I suppose that some system variable stores the value for "default folder viewer" but I do not know where. I will try to go for it and return later with the answer.
Hope that it helps.
EDIT: I did some quick digging around and to open the folder the following should do the trick:
System.Diagnostics.Process.Start(Environment.GetEnvironmentVariable("WINDIR") +
#"\explorer.exe", pathToFolder);
Where first argument is a path to classical windows explorer and second is the actual path to the folder itself.
It seem that widows does not by itself hold path to other "folder viewer" (such as Total Commander etc.), so this way is probably off the table.
Try this:
var psi = new System.Diagnostics.ProcessStartInfo() { FileName = pathToFolder, UseShellExecute = true };
System.Diagnostics.Process.Start(psi);
I usually use this to open file/directory:
public static void OpenFile(string path, bool isDirectory = false)
{
if (string.IsNullOrEmpty(path)) return;
if ((isDirectory && Directory.Exists(path)) || (!isDirectory && File.Exists(path)))
{
ProcessStartInfo pi = new ProcessStartInfo(path);
pi.Arguments = Path.GetFileName(path);
pi.UseShellExecute = true;
pi.WindowStyle = ProcessWindowStyle.Normal;
pi.Verb = "OPEN";
Process proc = new Process();
proc.StartInfo = pi;
proc.Start();
}
}
or
Process.Start("explorer.exe",path);
If this doesn't work it may be a permission issue after all.
You can set the working directory like this but you can't run the directory itself only files or exe
var startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = //working directory
Process proc = Process.Start(startInfo);
This error actually happens when there is a difference between the default behaviour of opening the file and the relative behaviour of opening the file.
For example, if you have selected the default application to open .pdf files as Internet Explorer, and you are trying to open the same file using Process.Start() method. You will receive an exception because as per the default operations it should open that file in Internet Explorer and your application is trying to open it using Adobe reader.
To rectify this set the default application for the .pdf file as Adobe Reader and you won't receive this error any more.
You can do this by, right-clicking on the file and then select, Default program or App. Further, select the default Program or App from the list of available programs and then select the Always use the selected program/App to open files of this type.
There are similar questions regarding this which were asked earlier. But I couldn't find a proper solution yet.
I have an application which uses a user defined dll library(Cplus_Function_Library.dll). Imagine that the application is launched already to customers. And if there's a new version of the dll available the application will automatically download it and replaces the old one. There's no problem there.
Now I want to create new dll libraries(a lot) and upload it in the sameplace where the Cplus_Function_Library.dll new version exsists(ex: http path/FTP server). And then I can add them by reference in the Cplus_Function_Library.dll. It's also clear. But my question is how can I download all the dll files in this path without giving the file names one by one inside my updater function? Because when I launch the app these files are not known.(updater function is included inside the application.)Is there an easy way to download all the dll files from a specified path without much hassle?
My current update function can be seen below.
Uri pathToNewVerNo = new Uri("//....../New_version.txt"); //Path to the new version number
Uri pathToCurrentVerNo = new Uri("...../Current_version.txt"); //Path to the current version number
Uri pathToDownload = new Uri(".....new_library.dll");
StreamReader readNewVer; //To read the new version number
StreamReader readCurVer; //To read the current version number
StreamWriter writeToCurVer;
WebClient verNew = new WebClient(); //will be used to download the New_version .txt file
WebClient verCur = new WebClient(); //will be used to download the Current_version .txt file
WebClient update = new WebClient(); //will be used to download the new dll file
verNew.DownloadFile(pathToNewVerNo, "New_version.txt"); //Download the New_version .txt file
readCurVer = new StreamReader("Current_version.txt"); //open Current_version.txt file to read
current_Version = readCurVer.ReadLine(); //assign the value to a string
readCurVer.Close(); //close the file
readNewVer = new StreamReader("New_version.txt"); //open New_version.txt file to read
new_Version = readNewVer.ReadLine(); //assign the value to a string
readNewVer.Close(); //close the file
current_ver_doub = Convert.ToDouble(current_Version); //convert the string value to a double
new_ver_doub = Convert.ToDouble(new_Version);
if (new_ver_doub > current_ver_doub) //check if the new version number is greater than the current version number
{
obj.SBO_Application.StatusBar.SetText("Please wait update in process", BoMessageTime.bmt_Medium, BoStatusBarMessageType.smt_Warning);
writeToCurVer = new StreamWriter("Current_version.txt"); //open the current_version.txt to write
writeToCurVer.Write(new_Version); //update with new version number
writeToCurVer.Close(); //close the file
update.DownloadFile(pathToDownload, "new_library.dll"); //download the new .dll file
//*************There will be a roll back functionality added in the future in case if the updated dll file is currupted.*****************
File.Replace("new_library.dll", "Cplus_Function_Library.dll", "Cplus_Function_Library.dll.bac", false); //make a back up file of the old .dll file and replace it
obj.SBO_Application.MessageBox("Update Successful. Please restart the AddOn", 1, "Ok");
try
{
foreach (Process proc in Process.GetProcessesByName("cplus_Global"))
{
proc.Kill();
proc.WaitForExit();
}
}
catch (Exception ex)
{
obj.SBO_Application.MessageBox(ex.Message, 1, "Ok");
}
}
else
{
// SBO_Application.MessageBox("No Update Available", 1, "Ok");
}
}//End of updater_cplus function */
First of all you need to ask yourself whether you absolutely want to reinvent the wheel, and are ready to tackle all problems you will encounter while doing that, such as the problem you now ran into. There are plenty of existing solutions to include installers and updaters with your software.
Anyway, to answer your question:
how can I download all the dll files in this path without giving the file names one by one inside my updater function?
You casually mention it should work over HTTP and FTP. The former has no formal "directory listing" command, so you'll have to invent that yourself. The latter does, but requires the directory to only contain relevant files, or you need to create a whitelist and/or blacklist to include or exclude certain files.
The easiest solution would be to define your version file format so as to include the list of files to download. Then you fetch the version file, interpret it and request the files mentioned in it.
Acording to CodeCaster's idea I put all my dll files inside one folder and the new_version text file in another folder. This way I made sure different file formats won't get mixed up.
The next step was to read the new_version.txt from the ftp server and compare the value with the current version. If it's greater than the later I took all the file names of dll files to a list. Then easily you can download the files one by one to your desired location.
I would like to programmatically open a document from a SharePoint URL.
I have the following code:
Process wordProcess = new Process();
wordProcess.StartInfo.FileName
= "http://sharepoint/blank_site_1/document library 1/word document.docx";
wordProcess.StartInfo.UseShellExecute = true;
wordProcess.Start();
This opens a webbrowser window and downloads the file, which is not what I want. If I append
wordProcess.StartInfo.Verb = "OpenAsReadOnly"
as per (the documentation) I get a Win32 Exception "The parameter is incorrect" at wordProcess.Start(), despite the verb being present in wordProcess.StartInfo.Verbs when examining in the debugger.
I have a POC which does this by extracting the default program from the registry, building a command and starting the program with the filename, but I'd rather not go down that route if this can be easily solved, as all I want to do is open a file (the path of which just happens to look like a URL) with the default program.
Just a guess, try this:
wordProcess.StartInfo.FileName = "winword.exe";
wordProcess.StartInfo.Arguments = "\"http://sharepoint/blank_site_1/document_library_1/word document file.docx\"";
I want to zip a file using PZKip in C# .net. I am using VS 2008. Can any one of you please help me with a C# .net code example.
When you say PKZip, does that mean you actually have the executable and you want to use that to ZIP a file? If that's the case, you can call the EXE through C# rather easily:
ProcessStartInfo startInfo = new ProcessStartInfo("pkzip.exe", "output.zip /add file1.txt file2.jpg file3.png");
startInfo.CreateNoWindow = true; // Let's not show the DOS box
// Execute the process
Process zipProcess = Process.Start(startInfo);
zipProcess.WaitForExit();
I don't know what the parameters, specifically, are for pkzip, but you can probably figure that out quite easily.
Now, if on the other hand you're asking about how to compress a file programmatically in C# to ZIP format, I would recommend you grab SharpZipLib. It supports several formats, including Zip, Gzip, BZip2, and Tar. It comes with sample code and it's open source.
You can grab it here: http://www.icsharpcode.net/opensource/sharpziplib/
Just in case you don't want to use PKZIP anymore and you don't want to use sharpziplib, .NET has built in compression classes:
http://msdn.microsoft.com/en-us/library/system.io.compression.aspx
If you must use PKZip then try this basic example:
static void Main(string[] args)
{
var p = new Process();
p.StartInfo.FileName = #"Path to pkzip.exe";
p.StartInfo.Arguments = "the args";
p.Start();
}