I am trying to open a file which a user can set. In other words it will never be a set path or file. so when the user has selected the file they want to open this button below will open it. I have declared l1 and p1 as public strings.
public void button4_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
l1 = Path.GetFileName(openFileDialog1.FileName);
p1 = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
}
public void button2_Click(object sender, EventArgs e)
{
//p1 = directory path for example "C:\\documents and settings\documents"
//l1 = filename
Process.Start(p1+l1);
}
So just to review I want to open up the file just using the directory path and filename. Is this possible? I can just have p1 there and it will open an explorer showing me that directory. Thank you for looking.
Yes it will work, but I would recommend you update your code this instead:
var path = Path.Combine(p1, l1);
Process.Start(path);
You shouldn't use string concatenation to combine a directory and filename. In your case the resulting string will look like this:
C:\documents and settings\documentsfilename
^^
this is wrong
Instead use Path.Combine.
string path = Path.Combine(p1, l1);
Process.Start(path);
Why don't you simply do this: -
public void button4_Click(object sender, EventArgs e)
{
string fileNameWithPath;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
fileNameWithPath = openFileDialog1.FileName;
}
}
public void button2_Click(object sender, EventArgs e)
{
Process.Start(fileNameWithPath);
}
Related
I tried to use the method
System.IO.File.Move(sourceFile,newFilePath);
to rename a File, but I always get the same error message, that my file couldn't be found.
I tried to manually write the source Path
System.IO.File.Move(#"D:\Users\XXX\Desktop\TestOrHMoin",#"D:\Users\XXX\Desktop\TestOrHMoinNEW");
but I still get the same ERROR Message.
This is my full code
`
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfiledialog = new OpenFileDialog();
if (openfiledialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
oldFilePath = openfiledialog.FileName;
listBox1.Items.Add(oldFilePath);
}
}
private void button2_Click(object sender, EventArgs e)
{
newFilePath = textBox1.Text;
oldFilePath = oldFilePath.Remove(oldFilePath.Length-newFilePath.Length);
newFilePath = oldFilePath + newFilePath;
string sourceFile = #oldFilePath;
string newFile = #newFilePath;
MessageBox.Show(sourceFile);
System.IO.File.Move(sourceFile,newFilePath);
// This part is the real code, the above Part is for debugging/testing
//System.IO.FileInfo fi = new System.IO.FileInfo(sourceFile);
//if (fi.Exists)
//{
// fi.MoveTo(newFilePath);
// MessageBox.Show("Erfolgreich geƤndert");
//} else { MessageBox.Show("Abbruch"); }
}
`
oldFilePath = oldFilePath.Remove(oldFilePath.Length-newFilePath.Length);
was wrong
I needed to declare another variable, so oldFilePath gets untouched. It was my error. I still don't know, why the manuell direction got an error.
I am creating an app that i need to copy some png files.
I already have searching with many keywords, finding many solutions and none of them worked, so i decided to ask here.
There is the code,it uses a windows forms and "this" refers to this window
private void button2_Click(object sender, EventArgs e)
{
//yes i commented them to solve why the file was not copying
//try
{
FileInfo x = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\some_location");
x.CopyTo(textBox1.Text);
}
//catch
{ }
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Title = "Select Image To Load";
openDialog.Filter = "Text Files (*.png)|*.png" + "|" + "All Files (*.*)|*.*";
if (openDialog.ShowDialog() == DialogResult.OK)
{
string PathData = openDialog.FileName;
textBox1.Text = PathData;
}
}
I have gotten several different errors, but most common there is:
System.UnauthorizedAccessException
It looks a bit weird to me that you're using an OpenFileDialog to select a destination. I'd assume you'd want to either do it the other way around:
private void button2_Click(object sender, EventArgs e)
{
//yes i commented them to solve why the file was not copying
//try
{
FileInfo x = new FileInfo(textBox1.Text);
x.CopyTo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\some_location");
}
//catch
{ }
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Title = "Select Image To Load";
openDialog.Filter = "Text Files (*.png)|*.png" + "|" + "All Files (*.*)|*.*";
if (openDialog.ShowDialog() == DialogResult.OK)
{
string PathData = openDialog.FileName;
textBox1.Text = PathData;
}
}
or use SaveFileDialog instead.
I am trying to make a program for the car retail firm I work at. When people deliver their cars, a guy has to take pictures of the damages with a Lenovo miix.
The program I've made so far is not smart enough.
It goes like, you write the numberplate in a box, then create a folder with the text from the box.
Then you start the camera, take a picture and save it, and manually have to find the folder and name the file.
Is there a way I can make it smarter with just 2 buttons, one to start the camera and another one to take the picture, and then it automatically saves it in a folder named as the numberplate, and files named 1,2,3,4 and so on.jpg ?
this is my code so far:
namespace Europcar_skade_camera
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection webcam;
private VideoCaptureDevice cam;
private void Form1_Load(object sender, EventArgs e)
{
webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach(FilterInfo VideoCaptureDevice in webcam)
{
comboBox1.Items.Add(VideoCaptureDevice.Name);
}
comboBox1.SelectedIndex = 1;
}
private void button1_Click(object sender, EventArgs e)
{
cam = new VideoCaptureDevice(webcam[comboBox1.SelectedIndex].MonikerString);
cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
cam.Start();
}
private void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bit = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bit;
}
private void button3_Click(object sender, EventArgs e)
{
if (cam.IsRunning)
{
cam.Stop();
}
}
private void button2_Click(object sender, EventArgs e)
{
saveFileDialog1.InitialDirectory = #"C:\tmp\";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image.Save(saveFileDialog1.FileName);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void nummerplade_TextChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(#"c:\tmp\" + nummerplade.Text);
}
}
}
Something like this:
Instead of using a SaveFileDialog, you could just create the file path based on the number plate value like this:
var filePath = System.IO.Path.Combine(#"c:\tmp", nummerplade.Text, fileNumber + ".jpeg");
The fileNumber var is the number of files allready in the folder + 1.
var fileNumber = Directory.GetFiles(...).Length + 1
string numberPlate = "21323412";
string path = #"C:\tmp\";
Directory.CreateDirectory(path + numberPlate); //to create folder
string dirpath = path + numberPlate; //to get name of fodler we created
You also mentioned that you want incremental names, there are many ways of doing it, personally i'd prefer large hashed name of file like so:
//generate GUID and convert to string.
string filename = Guid.NewGuid().ToString();
//to save picturebox image in folder and use ImageFormat.Your desired format
pictureBox1.Image.Save(dirpath + #"\"+filename+".jpeg", ImageFormat.Jpeg);
But if you want to stick with incremental names:
// you get last name in the folder:
//it gets last filename in folder but with path
var lastFilePath = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First();
//we remove path from filename and convert it to int.
int lastIndex = Int32.Parse(Path.GetFileNameWithoutExtension(lastFilePath));
//then we increment it by 1
int newName = lastIndex + 1;
//and save it
pictureBox1.Image.Save(dirpath + #"\"+filename+".jpeg", ImageFormat.Jpeg); //to save picturebox image in folder
With GUID finally it should look something like this:
string numberPlate = "21323412";
string path = #"C:\tmp\";
private void button2_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(path + numberPlate); //to create folder
string dirpath = path + numberPlate; //to get name of fodler we created
string filename = Guid.NewGuid().ToString();
pictureBox1.Image.Save(dirpath + #"\"+filename+".jpeg", ImageFormat.Jpeg); //to save picturebox image in folder
}
I am not able to save content of a text file while using the save option in notepad in visual c#
The code I have used is:-
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
string s = richTextBox1.Text;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.RichText);
}
}
You need to use RichTextBoxStreamType.PlainText
Hence code should be -
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
string s = richTextBox1.Text;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);
}
}
I've started to learn about System.IO in C# and I want to achieve something like this:
I have two buttons and one TextBox.
The first button event is supposed to use FolderBrowserDialog and let me choose a specific a folder.Then, to save its path in a variable.
The text box is supposed to get as a value the number of folders that I want to create in the choosen path.
The second button is going to create the number of folders(with different name each) written in the textbox at the first button specified path.
My buttons and textbox events so far:
...
int value;
String path;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog(this) == DialogResult.OK)
{
MessageBox.Show(fbd.SelectedPath);
path = folderBrowserDialog1.SelectedPath;//store selected path to variable "path"
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
value = Convert.ToInt32(textBox1.Text);//store the value from the textbox in variable "value"
}
private void button2_Click(object sender, EventArgs e)
{
if (!Directory.Exists(path))//if selected path exists
{
for(int i=0;i<value;i++)//trying to go through as folders as I wrote in the TextBox
{
Directory.CreateDirectory(path + "something");//is something wrong here, I guess.
}
}
}
My questions so far:
what's wrong with my code ?
how can I create each time the for(){} executes a folder with different name ?
I would appreciate any help
int value;
string path = null;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog(this) == DialogResult.OK)
{
MessageBox.Show(fbd.SelectedPath);
path = fbd.SelectedPath; //fbd not folderBrowserDialog1
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
value = Convert.ToInt32(textBox1.Text);//store the value from the textbox in variable "value"
}
private void button2_Click(object sender, EventArgs e)
{
if (path != null && Directory.Exists(path))
for(int i=0;i<value;i++)
Directory.CreateDirectory(Path.Combine(path,string.Format("SomeFolder{0}",i)));
}