2011-08-18 43 views
0

这里是我的问题:如何创建按钮新建一个文本文件,然后单击

我开发一个Windows应用程序

我将图像转换成文本格式,并将其保存到系统中的位置。

这是我的代码:

if (!System.IO.Directory.Exists("D:\\SaveImageOutPutFolder")) 
{ 
    System.IO.Directory.CreateDirectory("D:\\SaveImageOutPutFolder"); 
    doc.Save(ConfigurationSettings.AppSettings["SaveImagefolderPath"] [email protected]"\\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文件。

可以anyboy帮助这个,并给我一些示例代码?

+0

为什么大家都沉迷于CAPS LOCK? –

+0

什么是doc类型? 另外,图像内容是二进制格式。所以如果你阅读,你会得到一个bytearray数据。现在将它写入文本文件有什么用处? – Zenwalker

回答

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

要回答你的直接问题 - 你需要动态命名文件。既然你用“\ Mytxtfile.txt”这个名字静态命名它,它总是一样的。你可以做

一种选择是一样的东西:

ConfigurationSettings.AppSettings["SaveImagefolderPath"] + "\\" + Guid.NewGuid().ToString() + ".txt", Leadtools.Forms.DocumentWriters.DocumentFormat.Text, null); 
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"],也许这是一个错字?

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); 
相关问题