I can't manage to save a text file using the "saveFileDialog".
I have looked trough many forums and tutorials, but i cant get the right information from neither.
So far i have managed to open the Save file dialog and it actualy saves an empty text file with the right name and path,BUT, and this is the part i have trouble with, its EMPTY, and i dont know where you show what information to save on the file and what methods to use!
Here is the code :
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
saveFileDialog1->ShowDialog();
}
private: System::Void saveFileDialog1_FileOk(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e) {
System::IO::FileStream ^ fs = safe_cast<System::IO::FileStream^>(saveFileDialog1->OpenFile());
}
So can somebody tell me how and what to do?
Thanks in advance!
You use the save file dialog to get a path where to save your file. So basically you would need
saveFileDialog.showDialog();
String filename = saveFileDialog.FileName;
System.IO.StreamWriter file = new System.IO.StreamWriter(filename)
file.writeLine("This is a test");
file.close();
This is just a quick example :D
Related
Hallo! I have a specific question about my homework - C# window form app.
I enter data in text fields - first name, surname, last name, phone, email and date of birth (the last one is DateTimePicker control). And I save them with a button (
It's not actually saved at this stage, the data just goes to the other page of the app.)
Full names appear in ComboBox in other TabPage. When I select a name, I get the complete information about the person. I do that and it's works.(I'm using arrays).
The task done so far does not store any information. All input is lost when the application is closed.
The other part of the task gives me trouble - a link to an external text file in which the entered data is stored.
I create a text file and save information in it with this code :
private void button3_Click(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter("D:\\data.txt");
for (int k = 0; k <= i; k++)
{
txt.Write(Names[k] + "\n" + Phone[k] + "\n" + Email[k] + "\n" + Birthday[k] + "\n");
}
txt.Close();
MessageBox.Show("Data saved in data.txt file on drive D!", "Caution!", 0);
}
And the problem is that I can't open and read the file to load the data back into the app.
I need to be able to load the full names in my ComboBox and when I select a name to see all the personal information about that person.
I attach file with my app - page1 and page2 and my text file with store data.
Thanks in advance to everyone who responds :)
And the problem is that I can't open and read the file to load the data back into the app.
I need to be able to load the full names in my ComboBox and when I select a name to see all the personal information about that person.
I am trying to send an image file to api controller with postman. I am working with .Net 6.
The problem is an error always appears as in the image, postman screenshot
That is my code
namespace WebAPI.Controllers
public class Image
{
public IFormFile file { get; set; }
}
[Route("api/[controller]")]
[ApiController]
public class CarImagesController : ControllerBase
{
[HttpPost("add")]
public IActionResult Add([FromForm] Image file)
{
if (file != null)
{
return Ok();
}
return BadRequest();
}
}
If it is eligible to use, I want to change argument of Add method, i mean
public IActionResult Add([FromForm] IFormFile file)
and I want to delete Image class after that change.
Thanks for your help
Your code is correct, I create a sample using them, they all works well. The issue might relate that when using Postman, the key value contains spaces, such as file .
You can check the following screenshot: when using file key, everything works well, but if using file (with spaces), it will show the 400 error.
your code is all fine but you have an issue in postman. Check the formField key file. It has whitespace bcz you pressed enter there. Just remove any extra space and it should work.
I know it sounds stupid, but that's the issue. Postman takes whitespace and new lines seriously as key and values.
see the arrow and ... , that's the indicator.
this is my first question here on stackoverflow. Before i get into the question itself, i want to point out that i'm a beginner on Flutter and Dart and have no previous experience in mobile development.
Now, as the title says i'm having some issues while trying to extract the content of a text file and store the individual names inside the indexes of a list. The end goal of my application is to print a never ending list of these names, each separated to another by a comma. For example:
top model,
torcitóre,
torcolière,
tornitóre,
tosacàni,
tossicòlogo,
tour operator,
tracciatóre,
tranvière,
trattorìsta,
trattóre²,
trebbiatóre,
trecciaiòlo,
trevière,
tributarìsta,
trinciatóre,
trivellatóre,
truccatóre,
tubìsta,
turnìsta,
The text file, named jobs.txt contains more than a 1000+ italian job names. After several hours searching, all i have here is this (got from another user):
import 'dart:async';
import 'dart:convert';
import 'dart:io';
void main(List<String> arguments) {
try {
final _jobs = File('jobs.txt');
List<String> lines = _jobs.readAsLinesSync(encoding: ascii);
for (var l in lines) print(l);
} catch (Exception) {
print(Exception.toString());
}
}
jobs.txt is inside the same directory as the dart file, which is bin.
While trying to give the _jobs variable the relative path:
FileSystemException: Cannot open file, path = 'binjobs.txt' (OS Error: Impossibile trovare il file specificato.
, errno = 2)
With absolute path:
FileSystemException: Cannot open file, path = 'C:UsersUserDesktopdartdart_application_injobs.txt' (OS Error: La sintassi del nome del file, della directory o del volume non è corretta.
, errno = 123)
For reasons i don't know, if that even matters, in the path specified in the error message, the \ look like they don't even exist.
By the way, this is my project structure:
project structure
If your solution includes the usage of the Future class or async-await keywords, i'll be thankful if you could explain me how they work.
UPDATE: Look in the comments for the full solution to this issue. The verified answer isn't the full solution.
Create an assets folder in your project, in your pubspec.yaml specify the asset (make sure it's in your assets folder, that the file exists and is readable)
flutter:
assets:
- assets/jobs.txt
Now you can access your text file like so -
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
Future<String> loadAsset() async {
return await rootBundle.loadString('assets/jobs.txt');
}
When the function returns your text, you can split it by commas and loop through all the jobs as you wish
try using rootBundle
pubspec.yaml
assets:
- text_file/
import 'package:flutter/services.dart' show rootBundle;
Future<String> getData() async {
try {
return await rootBundle.loadString('text_file/four_words.txt');
} catch (e) {
throw (e.toString());
}
}
I'm trying to control Word through a c++ builder 5 application. I would like to
open a ".dot" model file created with Word and modify it. In the ".dot" model file
there are some fields. For example, Title, LastName, FirstName, Address
and so on, and I would like to modify these fields putting text into them and then
saving file with a new name, for example "Warning.doc" leaving the ".dot" file
unaltered.
I can open the file, count the number of fields it contains, but then
when it comes to replacing each field with a string I don't know how to do because
I don't have a complete documentation on OleFunction and OlePropertyGet methods. I attach my source code to this message, can anybody help me to solve this problem please?
try
{
my_word = Variant::CreateObject("word.application");
}
catch (...)
{
Application->MessageBox("Unable to obtain Word automation object",
"Error:",MB_OK | MB_ICONERROR);
}
my_word.OlePropertySet("Visible", (Variant)true);
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Variant this_doc;
Variant my_fields;
Variant test;
int k,field_count;
AnsiString test1;
AnsiString filename = "d:\\ProgrammaWord\\1-Avviso.dot";
my_docs = my_word.OlePropertyGet("Documents");
this_doc = my_docs.OleFunction("Open", filename);
my_fields = this_doc.OlePropertyGet("Fields");
field_count = my_fields.OlePropertyGet("Count");
for(k = 1; k <= field_count; k++)
{
test = my_fields.OleFunction("Item",(Variant)k);
test1 = test.OleFunction("Value"); //This instruction throws an exception
// "Value" is not a recognized parameter
// in this case
Memo1->Lines->Add(test1);
}
}
I never used word Ole but I used it for Outlook and Excel, I can't try it with word since I'm currently on OSX but you should try something similar as what I did.
The generic way of using Ole was to OleGetproperty() while you get the targeted field then OleSetProperty("action", ...).
for example when I wanted to change the color of the text in a particular cell of my excel document I used:
Variant _excel = Variant::CreateObject("Excel.Application");
Variant _workbook = _excel.OlePropertyGet("WorkBooks").OleFunction("Open", filename);
Variant _worksheet = _workbook.OlePropertyGet("WorkSheets", sheet);
_worksheet.OlePropertyGet("Cells", row, col).OlePropertyGet("Font").OlePropertySet("Color", color);
Here I instanciate an excel object, then I load a file into it (_workbook), then I select the _worksheet from the _workbook and I start my business.
Here comes the interesting part:
It concist of getting to a particular cell, getting the font object from it, and then setting the color of this font object.
Disclaimer: This is an example from my sources for excel, it's not directly related to your example, but maybe you can understand the principe with it. I can't try to figure out what you need because I have no windows right now.
Hope this can help you. Finding ressources for OLE can be fierce if you don't have the good patterns to look for.
I am able to install native apps in Google Glass now. I hope to know how to create a text file using native apps in Google Glass so that I can save data to text files.
Thanks!
Since Glass is an Android device and you are writing native Android apps for it, you should be able to write text files like you would on any other Android device.
The Android documentation on data storage should be helpful.
I tried the following code to create a file in Google Glass. The following code works with Android, which has a sd card. But does not work for Google Glass. Does anyone know how to create a text file in the Glass? Thanks!
public void SaveSubjectID(String filename, Context ctx) {
try {
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
// Toast.makeText(getBaseContext(), "files", Toast.LENGTH_SHORT).show();
//create a folder in a app.
//http://stackoverflow.com/questions/6911041/how-to-create-folder-into-sd-card-in-android
File rootDirect = new File(Environment.getExternalStorageDirectory() + "/SmartTexting");
if(!rootDirect.exists())
{ rootDirect.mkdir() ;
}
File subjectDirect = new File(Environment.getExternalStorageDirectory() + "/SmartTexting/"+"subject");
if(!subjectDirect.exists())
{ subjectDirect.mkdir() ;
}
//File sdcard = Environment.getExternalStorageDirectory();
File file = new File(subjectDirect,filename);
// Toast.makeText(getBaseContext(), "files created", Toast.LENGTH_SHORT).show();
FileOutputStream fos2 = new FileOutputStream(file, true);
String outline = "test"
+ "\n";
fos2.write(outline.getBytes());
fos2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I hope you have found out the correct answer by now, still this answer goes for all those who haven't found yet.
Code for writing file to google glass:
try {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File f = new File(path,"file_new.txt");
f.createNewFile();
Log.d("file pathhhh", "result"+f.getAbsolutePath());
Log.d("file created", "result"+f.createNewFile());
FileOutputStream fOut = new FileOutputStream(f);
//FileOutputStream fileout=openFileOutput(f);
OutputStreamWriter outputWriter=new OutputStreamWriter(fOut);
//writer = new FileWriter(file1);
//writer.write(text.toString());
outputWriter.write(text.toString());
/** Closing the writer object */
outputWriter.close();
Log.d("success", "success"+Environment.getExternalStorageState()+Environment.getStorageState(path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The key is to write file in DCIM folder and dont expect the file will be created then only.
To see whether the file has been created or not, shut down the glass and power it on again, you will find the new file in the DCIM folder