c# wpf only 1 file gets selected - c#

I created a WPF app in which I want to select many files from any given directory. The problem is that whenever I try to select multiple files, it will just copy the first file exactly as many times as many files I selected, instead of giving me all the different files.
What am I doing wrong?
TextFile textFile = new TextFile();
string[] arrAllFiles;
private void btnOpenFiles_Click(object sender, RoutedEventArgs e)
{
Stream myStream;
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter = "All Files (*.*)|*.*";
choofdlog.Multiselect = true;
if (choofdlog.ShowDialog() ==true)
{
//string sFileName = choofdlog.FileName;
arrAllFiles = choofdlog.FileNames; //used when Multiselect = true
}
//add all files in textbox
for (var i = 0; i < arrAllFiles.Length; i++)
{
textFile.files.Add(choofdlog);
myStream = textFile.files[i].OpenFile();
StreamReader reader = new StreamReader(myStream);
textFile.readFile.Add(reader);
lbFiles.Items.Add(arrAllFiles[i]);
}
}

i only use c# occasionally, so i can't give you a syntactical guarantee, but what i see at first glance is this
textFile.files.Add(choofdlog);
should be
textFile.files.Add(arrAllFiles[i]);
or
textFile.files.Add(choofdlog[i]);

Related

Writing Binary file at certain address

What I'm trying to do is read a blank file with no extension. From there, open the file and read it at a certain offset. Here's what I've done for that:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open 234cec File";
if (ofd.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(File.OpenRead(ofd.FileName));
BinaryReader br = new BinaryReader(File.OpenRead(ofd.FileName));
string Texture1 = null;
for (int i = 0x2D670DE; i <= 0x2D6712F; i++)
{
br.BaseStream.Position = i;
Texture1 += br.ReadChar().ToString();
}
br.Close();
textBox1.Text = Texture1;
}
else
{
MessageBox.Show("Error");
}
}
The program works just fine and displays text in a textbox from what it read.
However, I want to be able to write back in the file, from what's in the textbox it read, with a Save button.
i.e. from what I modify in the textbox, then have it save back to my file (at the specified address) WITHOUT changing the file size (like replacing what's there).
The file I'm reading is a kinda big file like 120MB and it doesn't just contain text, it also contains other hex/code and such.
My problem is, I'm clueless as to what line I should do for my Save button after I modify what it read in the Textbox. Any help?

functionality should apply to all pages but it's applying only in one place

I have a couple of functionalities like Add column, Merge the columns, Split the columns and Find and Replace options in a form.
I have a requirement called where i need to read a file and display in datagridview.
NOTE: A file contains 1000 lines but i have to show only 100 at the time of loading and if we want to view next records, we have click NEXT button (it loads the next 100 lines).
I'm done with this requirement and it's working fine.
Now the problem is If i use any functionality like Merge the 2 columns, it's merging 2 columns and giving result for only 100 lines in first page not for whole 1000 lines.
Can anyone resolve my issue?
Below is the code i'm pasting for merging 2 columns:
FORM1:
private void btnMerge_Click(object sender, EventArgs e)
{
frmMerge fs = new frmMerge(dataGridView1);
fs.cmbColumn1.DataSource = cmbList;
for (int i = 0; i <= cmbList.Count - 1; i++)
{
fs.cmbColumn2.Items.Add(cmbList[i]);
}
fs.ShowDialog();
}
Browse button code (where i select the file and read it into datagridview):
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "Desktop";
openFileDialog1.Filter = "dat files (*.DAT)|*.DAT|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
FileName = openFileDialog1.FileName;
string text = System.IO.File.ReadAllText(FileName);
datfile = text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
//Added on 2015-12-02
maxRec = datfile.Length - 1;
PageCount = maxRec / pageSize;
LoadPage(MyFOrmat);
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}

OpenFileDialog C# Asking for opening twice?

I have used a Telerik Browse Editor to open a file on my program. But for some reason it is asking me for the file twice. From the code below can anyone see why?
private void radBrowseEditor1_ValueChanged(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Title = #"Open .HRM File";
openFileDialog.InitialDirectory = #"C:\Users\mike\Desktop";
openFileDialog.Filter = #"HRM files (*.hrm)|*.hrm|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
}
using (StreamReader reader = new StreamReader(openFileDialog.FileName, System.Text.Encoding.Default))
{
HRM.Active.Raw = reader.ReadToEnd();
}
}
}
Probably something really simple. Pretty sure that code I used when I had a toolstrip before I installed Telerik and it worked fine.
After reading this, http://www.telerik.com/help/winforms/editors-browse-editor-working-with.html, it seems like you don't even need to bother with the openFileDialog. It does look like you should check that the value is not blank.
private void radBrowseEditor1_ValueChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(radBrowseEditor1.Value.ToString()))
{
using (StreamReader reader = new StreamReader(radBrowseEditor1.Value.ToString(), System.Text.Encoding.Default))
{
HRM.Active.Raw = reader.ReadToEnd();
}
}
}

Initial directory for OpenFileDialog

The file dialog has to open the last directory location that was used before it was shut down, but I have no idea how to do this. My colleague only shows me the example of word, when you click "file" it shows the last used files, he told me to use a register or an INI file, which I have never used before.
Here is the code I am using:
string f_sOudeLocatie = #"D:\path\is\classified";
private void btBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Zoek de CSV file";
fdlg.InitialDirectory = f_sOudeLocatie;
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
tbGekozenBestand.Text = fdlg.FileName;
tbVeranderNaamIn.Text = Path.GetDirectoryName(fdlg.FileName);
f_sOudeLocatie = Path.GetDirectoryName(fdlg.FileName);
f_sSourceFileName = fdlg.FileName;
f_sDestFileName = Path.GetFileName(Path.GetDirectoryName(fdlg.FileName)) + ".csv";
btOpslaan.Enabled = true;
tbVeranderNaamIn.ReadOnly = false;
}
}
if you'll create the OpenFileDialog outside the button click event it should remember the last folder you've been
string f_sOudeLocatie = #"D:\path\is\classified";
OpenFileDialog fdlg = new OpenFileDialog();
public Form1()
{
InitializeComponent();
fdlg.Title = "Zoek de CSV file";
fdlg.InitialDirectory = f_sOudeLocatie;
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
}
private void btBrowse_Click(object sender, EventArgs e)
{
if (fdlg.ShowDialog() == DialogResult.OK)
{
fdlg.InitialDirectory = fdlg.FileName.Remove(fdlg.FileName.LastIndexOf("\\"));// THIS LINE IS IMPORTENT
tbGekozenBestand.Text = fdlg.FileName;
tbVeranderNaamIn.Text = Path.GetDirectoryName(fdlg.FileName);
f_sOudeLocatie = Path.GetDirectoryName(fdlg.FileName);
f_sSourceFileName = fdlg.FileName;
f_sDestFileName = Path.GetFileName( Path.GetDirectoryName(fdlg.FileName) ) + ".csv";
btOpslaan.Enabled = true;
tbVeranderNaamIn.ReadOnly = false;
}
}
You need to set
fdlg.RestoreDirectory = false;
Reason:
RestoreDirectory property makes sure that the value in
Environment.CurrentDirectory will be reset before the OpenFileDialog
closes. If RestoreDirectory is set to false, then
Environment.CurrentDirectory will be set to whatever directory the
OpenFileDialog was last open to. As explained here
You can use registry to store the last directory location. And each time you open the file dialogue, get the value from the registry and set as the default location. When it closed store the location back to registry.
This code project article explains you well about reading and writing to registry
ReadWriteDeleteFromRegistry
If you choose to use INI file, some search will give you examples of how to read and write from INI file

Reading every paragraph in text file alone

I want to read a text file which contains more than one paragraph separated by new lines. How to read every paragraph alone in RichTextBox and how to transfer to the next paragraph by button next and back to the first paragraph by button previous designed in the form. My code
private void LoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dialog.Title = "Select a text file";
dialog.ShowDialog();
if (dialog.FileName != "")
{
System.IO.StreamReader reader = new System.IO.StreamReader(dialog.FileName);
string Text = reader.ReadToEnd();
reader.Close();
this.Input.TextChanged -= new System.EventHandler(this.Input_TextChanged);
Input.Clear();
Input.Text = Text;
}
}
Use this code.
var text = File.ReadAllText(inputFilePath);
var paragraphs = text .Split('\n');
paragraphs will be an array of strings containing all the paragraphs.
Use String.split() to split it at '\n'.
Afterwards iterate through the array on the Button next.
private string[] paragraphs;
private int index = 0;
private void LoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter =
"txt files (*.txt)|*.txt|All files (*.*)|*.*";
dialog.Title = "Select a text file";
dialog.ShowDialog();
if (dialog.FileName != "")
{
System.IO.StreamReader reader = new System.IO.StreamReader(dialog.FileName);
string Text = reader.ReadToEnd();
reader.Close();
this.Input.TextChanged -= new System.EventHandler(this.Input_TextChanged);
Input.Clear();
paragraphs = Text.Split('\n');
index = 0;
Input.Text = paragraphs[index];
}
}
private void Next_Click(object sender, EventArgs e)
{
index++;
Input.Text = paragraphs[index];
}
(I know this might not be the most elegant solution but it should give an idea on what to do.)

Categories