C# open file with existing running process using system.diagnostics process - c#

I initially thought that just maybe the process would use the existing process . but . it does not . the result of the following code starts 2 sepearte sessions of the CAD software - I need to open multiple files in one existing session of the CAD software .. I tried this first:
Process.Start(#"C:\Program Files\Autodesk\AutoCAD 2018\acad.exe",
"/product \"C3D\" " + #"C:\folder\1.dwg");
Process.Start(#"C:\Program Files\Autodesk\AutoCAD 2018\acad.exe",
"/product \"C3D\" " + #"C:\folder\2.dwg");
Then I did this, which works ... but I would still prefer to open a file in an existing process ... I guess the only way is to use interop ... but I would need to add an interop for each and every software app
Process.Start(#"C:\Program Files\Autodesk\AutoCAD 2018\acad.exe",
"/product \"C3D\""
+ " " + #"C:\folder\1.dwg"
+ " " + #"C:\folder\2.dwg"
+ " " + #"C:\folder\3.dwg"
);

Related

How to mount a VHD with only one partition?

I'm developing a project and I need the code in C# to assemble a vhd, and assign a letter to it. During the process of creating the vhd, I managed to do it via cmd by diskpart, but at that time I didn't assign a letter to it because the intention is to give the option to the user which letter he wants to mount. But now to mount I have a problem because I have no way of predicting which volume it will be mounted on. PS: The VHD only have and will only have one partition.
VHD Creation Code:
process.StandardInput.WriteLine("create vdisk file=" + Path.GetTempPath() + "storagedisk.vhd" + " maximum=" + size + " type=fixed");
process.StandardInput.WriteLine("select vdisk file=" + Path.GetTempPath() + "storagedisk.vhd");
process.StandardInput.WriteLine("attach vdisk");
process.StandardInput.WriteLine("convert gpt");
process.StandardInput.WriteLine("create partition primary");
process.StandardInput.WriteLine("format fs=ntfs label=\"ProSafe Disk\" quick");
process.StandardInput.WriteLine("detach vdisk");
process.StandardInput.WriteLine("exit");

Set up Scheuled Task to create Excel File and send email with attachment in C# Console application

I had a problem where a scheduled task would not send out an email with an attachment in a C# console application. It took me a long time to figure out the solution (it is working now), so I wanted to share what the two issues were so others can find the solution easily.
There were two problems:
1) When saving the file to the directory I could not use a relative directory like:
DirectoryInfo currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());
attachmentPath = currentDir.Parent.Parent.FullName + #"\Reports\" + startDate.ToString("yyyy.MM.dd") + " - " + endDate.ToString("yyyy.MM.dd") + ".xlsx";
I had to explicitly set the directory like:
attachmentPath = #"E:\reports\Excel Automated Reports\WebEx Daily Report\ExcelReportAutomation\Reports\" + startDate.ToString("yyyy.MM.dd") + " - " + endDate.ToString("yyyy.MM.dd") + ".xlsx";
After, I did that the file was being created and saved but still was not emailing it out. If I sent out an email without an attachment then the email would go out. The way to fix this issue is by setting the Start in (optional) option in the edit action for the scheduled task.
I repeat this is not a question but I am trying to lay out a solution for people who come across the same issue.

Generic GDI+ error when saving an image?

I want to save in image to other folder but it throw this Exception system.runtime.interopservices.externalexception (0x80040005)
this is my code :
pictureBox1.Image.Save(#"" + Application.StartupPath + "\\Image" + "".ToString() + #"\" + #"personeli_" + textBox4.Text + ".jpg");
This program works when I run it in visual studio but when I create setup file with InstallShield it throw an Exception .
I have a same problem and after a long search, I change the folder and my program works .I think if you use this code , your program run .
first I create a directory in Cdrive
if (!Directory.Exists((#"C:\Image")))
Directory.CreateDirectory((#"C:\Image"));
and after that I save picture into it .
new Bitmap(pictureBox1.Image).Save(#"C:\Image" + #"\" +/* your file name */+ ".jpg");
I hope it works for you ;)

Retrieval of txt file gives an error when deployed in IIS 7.0

I have a text file RHKLIS.txt which is placed in D:\ drive. I have found out some piece of code to retrieve the
path of the text file and it is working fine while running in the local host. I have deployed the code in some test machine using IIS 7.0
and tried accessing the same code but its not retrieving its giving the error as Sql transaction has closed in the line if (!File.Exists(path)).
So i got to know that path specified is an issue in the code.But still i am not aware of what type of error it is because there is a same text file placed in the D:\ drive in the same test machine.
in web.config
<appSettings>
<add key="LISpath" value="D:\RHKLIS.txt"/>
</appSettings>
.cs file
string path = ConfigurationManager.AppSettings["LISpath"].ToString();
FileStream fs = null;
if (!File.Exists(path))
{
fs = File.Create(path);
fs.Close();
}
StreamWriter sw = File.AppendText(path);
sw.WriteLine("O~" + billHospNo.Text.Trim() + "~ ~" + opBillNo.Text + "~" + billDate.Text + "~" + itemCode.Text + "~" + itemName.Text + "~" + p.Honourfic + "~" + p.Patient_Name + "~" + p.Gender + "~" + p.Age + "~" + "Y" + "~" + "~" + "~" + "~" + "~" + "~" + department.SelectedItem.ToString() + "~" + billDate.Text + "~" + itemDept.Value + "~" + deptName + "~" + yearOB + "~" + due);
sw.Close();
This is making the assumption you have you have full admin rights on the server.
Right-click on the folder, select properties. A property window of the folder will open - select the Security tag. You will see two lists - the top is for the users or groups and the bottom is the rights - click the Edit button under users.
Another widow will open - click the Add button - which will open another window. Make sure the locations is for the local machine and not a domain, then click advanced, and click the Find Now button. Look through the list until you see the IIS_IUSRS user and double-click on it. This will put the user in the bottom text filed of the selection window. Just click ok and it will automatically give that user the required permissions for the folder - then just click on ok all the way until all the windows are closed.
I hope this helps... This allow read-only access to that folder from IIS.
This sounds like a security problem. Firstly, the file being on the root of D: would mean that you would have open the whole D: to the IIS server (not good practice). Rather create a new folder, add the IIS_IUSRS user (with read & execute, List folder contents and Read permissions) - then put the file in that folder. Change the setting in the config file and that should work...

Program update code issue

Hey everyone.
I've developed a simple code to auto-update my program. The way it works is:
Program downloads a remote file that has the version string in it. If the version string is bigger than the program's, auto update initiates.
The program downloads the newest version of the program using a remote link with DownloadAsync.
The program creates a new batch file that kills the current app (The program itself), deletes the current program, and renames the new one to the application's name. Then, it runs the new updated application and deletes itself.
However, I'm facing an issue when the batch file is actually executed. Here is my code:
private void WC_Completed(object sender, AsyncCompletedEventArgs e)
{
StringBuilder Batch = new StringBuilder();
Batch.AppendLine("#echo off");
Batch.AppendLine("taskkill /IM " + Process.GetCurrentProcess().ProcessName + ".exe /F");
Batch.AppendLine("ping localhost > nul");
Batch.AppendLine("del /f " + (char)34 + Application.ExecutablePath + (char)34);
Batch.AppendLine("ren " + (char)34 + Application.StartupPath + #"\update.exe" + (char)34 + " " + Process.GetCurrentProcess().ProcessName + ".exe");
Batch.AppendLine((char)34 + Application.ExecutablePath + (char)34);
Batch.AppendLine("del %0");
File.WriteAllText(Application.StartupPath + #"\update.bat", Batch.ToString(), Encoding.Default);
Process.Start(Application.StartupPath + #"\update.bat");
}
For some reason, it doen't kill the current application, or it just takes too much, and the whole process is going crazy. It just runs the unupdated app because the renaming doesn't work, which causes a loop.
Can you please point out my error? I'm trying to see what's wrong!
Thank you!
There's an easier way to update a program if it consists of one executable file:
Rename a running executable using File.Move to something like my.exe.bak.
Put an updated executable in the place of the old one.
Launch the new copy using Process.Start("my.exe") and exit the old one.
Upon launch test if my.exe.bak exists and try to delete it. You won't succeed first time, but the backup will be deleted eventually.
This way you won't need any .bat trickery.
You can also enchance this algorithm by passing PID (Process ID) of the old instance to the new one through command line arguments and then using Process.GetProcessById(pid).WaitForExit(); to be able to delete my.exe.bak on the first launch and handle update process completion.

Categories

Resources