7

今私はこのコードを持っています:

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ループについて考えていますが、今はそれで遊んでいます。

4

8 に答える 8

12

存在しないファイルの名前を取得することは、メソッドの仕事のように聞こえます。

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;
}

複数のスレッドからこれを呼び出すと、競合状態が発生します。ファイル名を要求するアプリとスレッドが1つしかない場合、これは機能するはずです。

メソッドを使用するコードは次のようになります。

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); 
于 2012-04-12T21:02:22.370 に答える
9

@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);
于 2012-04-12T21:03:28.210 に答える
3

これは可能性です

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

このアプローチの利点は、ファイルシステムが1回だけ照会されることです。ファイル番号が大きくなった場合は、ファイル名をハッシュセットに追加して、チェックをさらに高速化することを検討してください。

var files = new HashSet<string>(Directory.GetFiles(scpath, "Screenshot_*.jpg"));
于 2012-04-12T21:06:08.020 に答える
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); 
于 2012-04-12T21:02:39.737 に答える
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がなくなるまでうまく機能するはずです...

于 2012-04-12T21:07:16.710 に答える
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);
于 2012-04-12T21:18:05.970 に答える
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");
于 2018-11-09T19:10:04.920 に答える
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;
}
于 2019-02-19T21:38:30.097 に答える