English 中文(简体)
用同一名称保存档案
原标题:Saving file with the same name
  • 时间:2010-07-21 11:41:33
  •  标签:
  • c#

我已利用这一方法挽救我的档案。

string m_strDate = DateTime.Now.ToString("MM/dd/yyyy");
m_strDate = m_strDate.Replace("/", "");
strPath += "/FileHeader_" + m_strDate + ".txt";

因此,我每天可以节省一份档案。 但是,如果我再造一段时期,该文本档案中的数据就被新的数据所取代。 现在,我需要的是,用一些名字和日期以及一些类似名字来拯救我的档案。

"/FileHeader_1" + m_strDate + ".txt"

等等。

最佳回答
string fileName = "/FileHeader_" + m_strDate + ".txt";
if (File.Exists(fileName))
{
  int index = 1;
  fileName = "/FileHeader_" + index + m_strDate + ".txt";
  while (File.Exists(fileName))
    fileName = "/FileHeader_" + ++index + m_strDate + ".txt";
}
问题回答
strPath = "/FileHeader_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";

如果档案存在,或检查:

strPath = "/FileHeader_{0}" + DateTime.Now.ToString("MMddyyyy") + ".txt";
if ( File.Exists( string.format( strPath, "" ) ){
  int i = 1;
  while( File.Exists(string.format( strPath, i ) ){ i++ ; }
  strPath = string.Format(strPath, i);
}
else {
  strPath = string.format( strPath, "" );
}
string head = Path.Combine(strPath, "FileHeader_");
string tail = DateTime.Now.ToString("MMddyyyy") + ".txt";

int index = 1;
string fileName = head + tail;

while (File.Exists(fileName))
{
    fileName = head + index + tail;
    index++;
}
    public string AddToFileNameUniqueNumber(string fileFullPath)
    {
        if (string.IsNullOrWhiteSpace(fileFullPath))
        {
            throw new ArgumentNullException(nameof(fileFullPath), "Path can t be null or empty.");
        }
        if (File.Exists(fileFullPath))
        {
            throw new ArgumentException(nameof(fileFullPath), "The specified path does not lead to the file.");
        }

        string fileDirectoryName = Path.GetDirectoryName(fileFullPath);
        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileFullPath);
        string fileExtension = Path.GetExtension(fileFullPath);

        string fullFileNameWithUniqueIndex = "";

        for (int i = 1; i < int.MaxValue; i++)
        {
            fullFileNameWithUniqueIndex = Path.Combine(fileDirectoryName, $"{fileNameWithoutExtension} ({i}){fileExtension}");
            if (!File.Exists(fullFileNameWithUniqueIndex))
            {
                break;
            }
        }

        return fullFileNameWithUniqueIndex;
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签