2013-06-19 67 views
0

在下面的代碼中,文件被保存在項目的debug文件夾中,我想將文件保存在通用指定文件夾下的appdata文件夾中!GetTempFileName()並保存到AppData文件夾

AViewModel vm = DataContext as AViewModel; 
var table = vm.FileSelectedItem; 

if (table != null) 
{ 
    var filename = System.IO.Path.GetTempFileName(); 
    File.WriteAllBytes(table.FileTitle, table.Data); 
    Process prc = new Process(); 
    prc.StartInfo.FileName = table.FileTitle; 
    prc.Start(); 
} 

//table.FileTitle is the name of the file stored in the db 
// eg:(test1.docx, test2.pdf, test3.txt, test4.xlsx) 
//table.Data is public byte[] Data { get; set; } property 
// which stores the files coming from the db. 

我在看GetFolderPath現在嘗試這樣的事情

System.IO.Path.GetTempFileName(
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); 

感謝您的回信!

+1

你想要做的事情還不清楚。 'filename'永遠不會被使用。它看起來像你忘了問你的問題... –

回答

8

GetTempFileName返回用戶臨時路徑中文件的完整路徑。您不能使用它在特定文件夾內創建文件。

既然你要在AppData文件夾中存儲已經,也許你更多的東西等之後:

var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "YourCompany\\YourProduct\\Output"); 

var filename = Path.Combine(path, table.FileTitle); 

File.WriteAllBytes(filename, table.Data); 

Process.Start(filename); 
2

如果你想創建應用程序數據下的隨機命名的文件,你可以嘗試

Guid.NewGuid().ToString("N") 

這會給你隨機字符串合理確定它是唯一的。對於應用程序數據文件夾下:

Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Guid.NewGuid().ToString("N")); 

注:至少把它放到一些子文件夾,應用程序數據與所有其他應用程序共享文件夾。

相關問題