2013-08-29 49 views
-3

我正在制作一个程序来分发给人。目前我使用:C#自动检测计算机名称/文件路径

bitmap.Save("C:/My OVMK Photos//OpenVMK" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg", ImageFormat.Jpeg); 

我想让它自动检测自己的电脑文件路径桌面所以它会保存到桌面上的文件夹。

我期待使用此代码:

string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
filePath =filePath [email protected]"\Error Log\"; 
string extension = ".log"; 
if (!Directory.Exists(filePath)) 
{ 
Directory.CreateDirectory(filePath); 
} 

我将如何实现呢?

+0

所以有它工作? –

+0

为什么不使用代码呢? –

+0

'我正在使用这段代码'好像你已经知道该怎么做了,那么答案是什么? – PoweredByOrange

回答

0

你有一切到位。只需将位图保存到您所创建的,而不是 "C:/My OVMK Photos//OpenVMK"

bitmap.Save(filePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg", ImageFormat.Jpeg); 
1

我假设它不能正常工作。您需要:

  1. 确保你已经在桌面
  2. 使用Path.Combine与“错误日志”文件路径结合在一个“错误日志”文件夹中,而不是拼接
0

使用这样的函数

void SaveToDesktop(Bitmap bitmap) 
{ 
    string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
    filepath = Path.Combine(filePath,"Error Log"); 
    if (!Directory.Exists(filePath)) 
    { 
     Directory.CreateDirectory(filePath); 
    } 
    filepath = Path.Combine(filepath, DateTime.Now.ToString("image_yyyyMMddHHmmss") + ".jpg"); 
    bitmap.Save(filepath, ImageFormat.Jpeg); 
} 

然后而是采用bitmap.Save

的文件路径做SaveToDesktop(bitmap);