How to create a new text file on button click
Here is my problem:
I am developing a windows application
I am converting an image into text format, and saving it into a system location.
This is my code:
if (!System.IO.Directory.Exists("D:\\SaveImageOutPutFolder"))
{
System.IO.Directory.CreateDirectory("D:\\SaveImageOutPutFolder");
doc.Save(ConfigurationSetting开发者_JAVA百科s.AppSettings["SaveImagefolderPath"] +@"\\MytxtFile.txt", Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null);
MessageBox.Show("folder created and image output saved");
}
else
{
doc.Save(ConfigurationSettings.AppSettings["SaveImagefolderPath"] + @"\\MytxtFile.txt", Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null);
MessageBox.Show("ImageFolder is available images are Saved into folder Now");
}
The problem is that it is not creating a new text file every time the button is clicked. It is writing to the same file. I want to create to a new txt file for every click.
Can anyboy help in this, and give me some sample code?
To answer your immediate question - you need to dynamically name the file. Since you're statically naming it with the "\Mytxtfile.txt" name, it's always going to be the same.
One option you could do is something like:
ConfigurationSettings.AppSettings["SaveImagefolderPath"] + "\\" + Guid.NewGuid().ToString() + ".txt", Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null);
You need to create or pass in a name, otherwise it will create the same file name every time - it's doing what you told it to do. Try this:
// Assuming you have a string named fileName that holds the name of the file
if (!System.IO.Directory.Exists("D:\\SaveImageOutPutFolder"))
{
System.IO.Directory.CreateDirectory("D:\\SaveImageOutPutFolder");
doc.Save(ConfigurationSettings.AppSettings["SaveImagefolderPath"] + "\\" + fileName, Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null);
MessageBox.Show("folder created and image output saved");
}
else
{
doc.Save(ConfigurationSettings.AppSettings["SaveImagefolderPath"] + "\\" + fileNAme, Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null);
MessageBox.Show("ImageFolder is available images are Saved into folder Now");
}
How and when the variable fileName
gets populated is up to you and the requirements of your program.
As zenwalker noted above, images are binary, so saving it to a text file is questionable; but as to your original question, the above should fix it.
EDIT
Another thing to be careful of - in your code you're explicitly checking for D:\SaveImageOutPutFolder, and creating it if it does not exist, but you're building the path based on the value of SaveImagefolderPath
in your config file. That could cause problems if the value in your config file is not D:\SaveImageOutPutFolder. Suggest you do something like this:
string outputFolder = ConfigurationSettings.AppSettings["SaveImagefolderPath"];
if (!System.IO.Directory.Exists(outputFolder)
{
System.IO.Directory.CreateDirectory(outputFolder);
MessageBox.Show("Folder created");
}
doc.Save(outputFolder + "\\" + fileName, Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null);
MessageBox.Show("ImageFolder is available images are saved into folder now");
You can use System.IO.Path.GetRandomFileName
method, here is a method I develop sometime ago:
public static string GetUniqueFileName(string rootDirectory, string extension)
{
if (rootDirectory == null)
{
throw new ArgumentNullException("rootDirectory");
}
if (!Directory.Exists(rootDirectory))
{
throw new DirectoryNotFoundException();
}
string fileName = null;
do
{
fileName = System.IO.Path.Combine(rootDirectory, System.IO.Path.GetRandomFileName().Replace(".", ""));
fileName = System.IO.Path.ChangeExtension(fileName, extension);
} while (System.IO.File.Exists(fileName) || System.IO.Directory.Exists(fileName));
return fileName;
}
Edit: How to use it and re-factoring your method:
string root = ConfigurationSettings.AppSettings["SaveImagefolderPath"];
string extension = ".txt";
bool newlyFolderCreated = false;
if (!System.IO.Directory.Exists(root))
{
System.IO.Directory.CreateDirectory(root);
newlyFolderCreated = true;
}
doc.Save(GetUniqueFileName(root, extension),
Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null);
if (newlyFolderCreated)
{
MessageBox.Show("folder created and image output saved");
}
else
{
MessageBox.Show("ImageFolder is available images are Saved into folder Now");
}
Also I don't know why youare using "D:\\SaveImageOutPutFolder"
and ConfigurationSettings.AppSettings["SaveImagefolderPath"]
at the same time, maybe it is a typo?
Assuming you know the file name of the original image file and have it available in a string variable (PathOfOriginalImageFile
) try
doc.Save(ConfigurationSettings.AppSettings["SaveImagefolderPath"] + "\\" +
Path.GetFileNameWithoutExtension (PathOfOriginalImageFile) + ".txt",
Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null);
IF you need different filenames even when the original filename is identical then try
doc.Save(ConfigurationSettings.AppSettings["SaveImagefolderPath"] + "\\" +
Path.GetFileNameWithoutExtension (PathOfOriginalImageFile) + "-" +
Guid.NewGuid.ToString() ".txt",
Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null);
精彩评论