0

Hello I would like to ask how I can create a text file with a specific name and gain access to the folder I'am saving into since I'am the administrator of the computer but I do not have access to save through C#. What i would like to do is iterate over buttons in a UI and get the names of the buttons and write them as text file names in a directory.

I would enjoy getting an answer how I can modify the name of the file I'am saving. My code so far looks like this.

 foreach (Button bt in mainCanvasGrid.Children)
{
   File.WriteAllLines()
}
  • just put a string variable and change it in each iteraton. What have you tried? – Federico Berasategui Nov 30 '13 at 12:11
  • I tried WriteAllLines() because I know it creates a file each time and this is exactly the behaviour I'm looking for - to create a file with the name of each button as the file name,but I cannot modify the name of the file I'm creating – Christo Christov Nov 30 '13 at 12:16

1 Answers1

1

just put a string variable and change it in each iteraton:

var myfile = @"C:\myfile"
foreach (Button bt in mainCanvasGrid.Children)
{
   File.WriteAllLines(myfile + bt.Name + ".txt", new []{"[yourtexthere]"});
}

BTW, the preferred approach to WPF is MVVM. You don't "iterate a collection of Buttons" in the UI simply because UI is NOT Data. Create a proper Data Model or ViewModel for your Data and use DataBinding to Bind the UI to your data, instead of manipulating the UI in procedural code.

Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154