2011-08-18 78 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); 
相關問題