如何在按钮单击时创建新的文本文件

时间:2011-08-18 06:21:03

标签: c#

这是我的问题:

我正在开发一个Windows应用程序

我正在将图像转换为文本格式,并将其保存到系统位置。

这是我的代码:

if (!System.IO.Directory.Exists("D:\\SaveImageOutPutFolder"))
{
    System.IO.Directory.CreateDirectory("D:\\SaveImageOutPutFolder");
    doc.Save(ConfigurationSettings.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");
}

问题是每次单击按钮时都不会创建新的文本文件。它写入同一个文件。我想为每次点击创建一个新的txt文件。

任何人都可以帮忙,并给我一些示例代码吗?

4 个答案:

答案 0 :(得分:1)

要回答您的直接问题 - 您需要动态命名该文件。由于您使用“\ Mytxtfile.txt”名称静态命名,因此它始终是相同的。

你可以做的一个选择是:

ConfigurationSettings.AppSettings["SaveImagefolderPath"] + "\\" + Guid.NewGuid().ToString() + ".txt", Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null); 

答案 1 :(得分:0)

您需要创建或传入一个名称,否则每次都会创建相同的文件名 - 它正在执行您告诉它的操作。试试这个:

// 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");
}

变量fileName的填充方式和时间取决于您和程序的要求。

正如上面提到的zenwalker,图像是二进制的,因此将其保存为文本文件是值得怀疑的;但至于你原来的问题,上面应该解决它。

修改 另外要注意的是 - 在你的代码中,你明确地检查D:\ SaveImageOutPutFolder,如果它不存在则创建它,但是你要根据配置中SaveImagefolderPath的值构建路径文件。如果配置文件中的值不是D:\ SaveImageOutPutFolder,则可能会导致问题。建议你做这样的事情:

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");

答案 2 :(得分:0)

您可以使用System.IO.Path.GetRandomFileName方法,这是我之前开发的方法:

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;
}

修改:如何使用它并重新分解您的方法:

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");
}

另外我不知道为什么你同时使用"D:\\SaveImageOutPutFolder"ConfigurationSettings.AppSettings["SaveImagefolderPath"],也许这是一个错字?

答案 3 :(得分:0)

假设您知道原始图像文件的文件名并将其提供给字符串变量(PathOfOriginalImageFile),请尝试

doc.Save(ConfigurationSettings.AppSettings["SaveImagefolderPath"] + "\\" +
Path.GetFileNameWithoutExtension (PathOfOriginalImageFile) + ".txt", 
Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null);

如果您需要不同的文件名,即使原始文件名相同,请尝试

doc.Save(ConfigurationSettings.AppSettings["SaveImagefolderPath"] + "\\" +
Path.GetFileNameWithoutExtension (PathOfOriginalImageFile) + "-" + 
Guid.NewGuid.ToString() ".txt", 
Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null);