自动增加文件名

时间:2012-04-12 20:54:34

标签: c#

现在,这是我的代码

int number = 0;
DirectoryInfo di = new DirectoryInfo(scpath + @"Screenshots\");

if (di.Exists) {

} else {
    di.Create();
}
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
bmpScreenShot.Save(di + "Screenshot_" + number, ImageFormat.Jpeg);

程序获取屏幕截图(有效),然后保存镜头。我想做的是,有程序检查,看看屏幕截图是否存在“Screenshot_ *”,如果没有创建它。如果是,则递增直到它达到“Screenshot_”结尾处未使用的数字 鉴于文件和递增更多,不知道如何解决这个问题。我正在考虑for循环,但我现在正在玩它。

8 个答案:

答案 0 :(得分:8)

获取不存在的文件名称听起来像是方法的作业。

string IndexedFilename(string stub, string extension) 
{
    int ix = 0;
    string filename = null;
    do {
        ix++;
        filename = String.Format("{0}{1}.{2}", stub, ix, extension);
    } while (File.Exists(filename));
    return filename;
}

如果您从多个线程调用此选项, 是竞争条件。 假设你在应用程序中只有一个应用程序和一个线程要求文件名,那么这应该可以工作。

使用该方法的代码如下所示:

string di = Path.Combine(scpath, "Screenshots");
if (!Directory.Exists(di) { 
    Directory.Create(di); 
} 
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width; 
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height; 
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight); 
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot); 
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
string filename = IndexedFilename(Path.Combine(di,"Shot_"),"jpg");
bmpScreenShot.Save(filename, ImageFormat.Jpeg); 

答案 1 :(得分:7)

像@Quintin所说,使用datetime作为文件名:

string filename = Path.Combine(
    di.FullName,
    String.Format("{0}.jpg", DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss")));
bmpScreenShot.Save(filename, ImageFormat.Jpeg);

答案 2 :(得分:2)

不使用数字作为区分屏幕截图的方法,而是使用时间戳:

string currentDT = string.Format("{0:D4}.{1:D2}.{2:D2}-{3:D2}.{4:D2}.{5:D2}",
                   DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day,
                   DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second)
bmpScreenShot.Save(di + "Screenshot_" + currentDT, ImageFormat.Jpeg); 

答案 3 :(得分:2)

这是一种可能性

string[] files = System.IO.Directory.GetFiles(scpath, "Screenshot_*.jpg");
string filename;
int i = 0;
do {
    filename = "Screenshot_" + ++i + ".jpg";
} while (files.Contains(filename));

答案 4 :(得分:2)

我会使用GUID ...

try{
    bmpScreenShot.Save(di + "Screenshot_" + Guid.NewGuid().ToString(), ImageFormat.Jpeg);
}catch(Exception e)
{ 
    //handle the problems here, for example if file already exists, try again
}

这应该可以正常运行,直到您用完了唯一的GUID ...

答案 5 :(得分:1)

public static string MakeUniqueFileName(string file)
{
    string dir = Path.GetDirectoryName(file);
    string fn;

    for (int i = 0; ; ++i)
    {
        fn = Path.Combine(dir, string.Format(file, i));

        if (!File.Exists(fn))
            return fn;
    }
}

像这样使用:

string file = scpath + @"Screenshots\" + "Screenshot_{0}.png";
bmpScreenShot.Save(MakeUniqueFileName(file), ImageFormat.Jpeg);

答案 6 :(得分:0)

这将创建output_0.jpg output_1.jpg ... output_n.jpg:

int filecount = 0;
string path = Environment.CurrentDirectory;
for (int i = 0; File.Exists(path + @"\output_" + i + ".jpg"); i++)
{
    filecount = i + 1;
}
File.Create(path + @"\output_" + filecount + ".jpg");

答案 7 :(得分:0)

private static string GetUniqueFile(string path, string file, string ext)
{
    int filecount = 0;
    int i = 0;
    for (i = 0; File.Exists(path + "\\" + file + "_" + i + "." + ext); i++)
    {
        filecount = i + 1;
    }

    return path + "\\" + file + "_" + i.ToString() + "." + ext;
}