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 ;)
Related
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"
);
I have a Winforms program that needs to log data points into a .CSV file. It's fairly simple, date/time and a double (the data), and go to the next line.
Here's what I have so far (not working, I get an error saying the file is busy/already open - however, it's empty)
if (!Directory.Exists(SavePath.Text + "\\LOG"))
Directory.CreateDirectory(SavePath.Text + "\\LOG");
string LogFileName = SavePath.Text + "\\LOG\\Seeing-Log-" + TimeNow.ToString("yyyy-MM-dd") + ".csv";
if (!File.Exists(LogFileName))
File.Create(LogFileName);
string LogString = TimeNow.ToString("yyyy-MM-dd-_HH-mm-ss") + "," + FWHM_Value.ToString("F:");
File.AppendAllText(LogFileName, LogString + Environment.NewLine);
It's that last line that generates the error.
Any idea what I am doing wrong?
thanks
Steve
File.Create returns an open FileStream to the file that's just been created. Either change your code to work with FileStream in both the non-existent and existent file cases, or just close the file after creating it:
if (!File.Exists(LogFileName))
File.Create(LogFileName).Close();
But, of course, if you check the documentation for AppendAllText:
Appends the specified stringto the file, creating the file if it does not already exist.
You'll realise that the above two lines are completely redundant anyway and can be removed:
if (!Directory.Exists(SavePath.Text + "\\LOG"))
Directory.CreateDirectory(SavePath.Text + "\\LOG");
string LogString = TimeNow.ToString("yyyy-MM-dd-_HH-mm-ss") + "," + FWHM_Value.ToString("F:");
File.AppendAllText(LogFileName, LogString + Environment.NewLine);
You can even use the free looging tools. Here is one 'log4net'
You can also write the csv file using this. I am assuming currently you are not using logging tool. it will work for you without any code for implementation .
http://element533.blogspot.in/2010/05/writing-to-csv-using-log4net.html
Have a great day!!
Replace
File.Create(LogFileName);
with
File.Create(LogFileName).Close();
see this to create empty file.
The file is locked when you create it. Just update your code to this:
File.Create(LogFileName).Close();
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...
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.
I have been using this line of code for the past few weeks to dynamically open a doc file once the user has created it and its been working fine...
System.Diagnostics.Process.Start(#"C:\\Users\\peter\\Desktop\\" + txtEditTitle.Text + ".doc");
but today, for some reason it gives me the following error:
Can anybody help?
As you can read in the exception the file is not there. It would not be wrong to check if the file exists before opening it.
As per your screenshot, it shows file is not there. Check your path for the file and make sure file exists:
string strPath = "C:\\Users\\peter\\Desktop\\" + txtEditTitle.Text + ".doc";
// strPath=#"C:\Users\peter\Desktop\" + txtEditTitle.Text + ".doc";
if (File.Exists(strPath))
{
System.Diagnostics.Process.Start(strPath);
}