Monday 20 March 2017
On 21:22 by Vardan Kumar in C# 15 comments
File versioning, saving file with unique file name in c#
File versioning allows a user to have several versions of a file simultaneously at a particular path. Not only it intimates a user of which is the latest version i.e generated at a later stage or an older version but also provides a unique file name in the same directory.
For example....
Suppose a report generation application is developed which saves a user-readable file as an output, as discussed in earlier posts also an efficient GUI(Graphical user interface) is the most interactive one i.e. a GUI in which a user feels to have control over the application. In such an application a user is generally provided with the privilege to select his/her desired path. It is a general tendency of a computer operating individual to select same path as well as name each time he/she wants to generate the report. Now suppose user chooses a file name as cspassion.xlsx and the same file name already exists at that path, so an efficient application would save the latest version of report as cspassion(1).xlsx or cspassion(2).xlsx to avoid exception or prompting user to select if he/she wants to replace the previous version of file or not, also this would take the control from your application to the operating system which should generally be avoided as much as possible.
Logic
- First of all user selected file name or path is sent as an argument to the method fetching unique file path or creating file versions.
- Now we will check if the file already exists at that directory or not
- If not then return that file path and save the file with user selected file name at his/her desired path.
- If yes then continue with the version generation.
- Now split and store directory,file name as well as extension in different identifiers.
- Initialize number as 1,if you want to display (2) as file version after first version and initialize to 0 if you want it as (1) after first version.
- Now we'll check if a version after initial version is generated using regular expression match
- If it matches then we'll extract file name from group 1 and number from group 2.
- Now we have number as well as file name we'll loop through while the file exists in that path.
- We'll increment number by 1 in each iteration
- Format the string as filename,(number) and extension.
- Return the file path and save the workbook.
I have generated five versions of same file named cspassion for reference in current directory. Refer to image below
File Versioning C# |
Source Code
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Oracle.DataAccess.Client;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Xml;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
string s_oRcConnectionString = null; //Click here to know how to set dynamic connection string c#
ToolTip tt = new ToolTip();
string uniquepath=String.Empty;
private void btn_Save(object sender, EventArgs e)
{
xlWorkbook.SaveAs(uniquepath);
}
private void btn_Browse_Click_1(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();// using save file dialog
saveFileDialog1.Filter = "Excel|*.xlsx";
saveFileDialog1.Title = "Save Excel report";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
lbl_Path.Text = saveFileDialog1.FileName;
tt.SetToolTip(lbl_Path, lbl_Path.Text); // click here to read how to set tool tip for controls
lbl_Path.Visible = true;
string path=lbl_Path.Text;
uniquepath=GetUniqueFilePath(path);
}
public static string GetUniqueFilePath(string filepath)
{
if (File.Exists(filepath))
{
string fold = Path.GetDirectoryName(filepath);
string filename = Path.GetFileNameWithoutExtension(filepath);
string extension = Path.GetExtension(filepath);
int number = 1;
Match regex = Regex.Match(filepath, @"(.+) \((\d+)\)\.\w+");
if (regex.Success)
{
filename = regex.Groups[1].Value;
number = int.Parse(regex.Groups[2].Value);
}
do
{
number++;
filepath = Path.Combine(folder, string.Format("{0} ({1}){2}", filename, number, extension));
}
while (File.Exists(filepath));
}
return filepath;
}
}
}
Subscribe to:
Post Comments (Atom)
Search
Popular Posts
-
Troubleshooting Cisco VPN client Before starting troubleshooting, Let us see what VPN is and what it requires to perform its intended f...
-
Evolution-Mobile Phones With the development of portable technology,wireless communication has so evolved that (According to the announce...
-
File Versioning C# File versioning, saving file with unique file name in c# File versioning allows a user to have several versions of ...
-
Text Box Hint in c# Windows Form Application Text Box Hint in c# Windows Form Application While developing a windows form applicat...
-
Unable to set the Freeze Panes property of Window Class C# It is generally easy to resolve the compile time errors because the reason fo...
Yalova
ReplyDeleteHatay
Muş
Bursa
Mersin
THXCUZ
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
E6G1P
1F5BC
ReplyDeleteMersin Evden Eve Nakliyat
Ardahan Evden Eve Nakliyat
Bolu Parça Eşya Taşıma
Tunceli Parça Eşya Taşıma
Bursa Evden Eve Nakliyat
20933
ReplyDeleteÇanakkale Evden Eve Nakliyat
Aksaray Evden Eve Nakliyat
Antep Evden Eve Nakliyat
Van Evden Eve Nakliyat
Kastamonu Evden Eve Nakliyat
DFBAB
ReplyDeleteGiresun Şehirler Arası Nakliyat
Bitlis Şehirler Arası Nakliyat
Artvin Lojistik
Şırnak Şehir İçi Nakliyat
Lovely Coin Hangi Borsada
Osmo Coin Hangi Borsada
Bitranium Coin Hangi Borsada
Çerkezköy Marangoz
Konya Parça Eşya Taşıma
5F8F3
ReplyDeletereferans kodu %20
D7A2D
ReplyDeletegiresun mobil sohbet chat
konya mobil sohbet siteleri
şırnak muhabbet sohbet
rize görüntülü sohbet odaları
aksaray sohbet siteleri
kızlarla rastgele sohbet
bingöl sesli sohbet mobil
mobil sohbet siteleri
amasya sesli sohbet odası
DD140
ReplyDeletecanlı sohbet odaları
kocaeli mobil sohbet et
diyarbakır ücretsiz sohbet uygulaması
bitlis görüntülü sohbet odaları
bedava sohbet chat odaları
rize parasız sohbet siteleri
Izmir Canlı Görüntülü Sohbet
artvin sesli sohbet siteleri
eskişehir görüntülü sohbet kadınlarla
8749B
ReplyDeleteGörüntülü Sohbet Parasız
Binance Kaldıraçlı İşlem Nasıl Yapılır
Binance Hesap Açma
Ceek Coin Hangi Borsada
Twitch Takipçi Satın Al
Bitcoin Yatırımı Nasıl Yapılır
Gate io Borsası Güvenilir mi
Binance Borsası Güvenilir mi
Likee App Takipçi Satın Al
EFE49
ReplyDeleteBinance Nasıl Oynanır
Bitcoin Madenciliği Siteleri
Binance Referans Kodu
Pinterest Takipçi Hilesi
Yeni Çıkan Coin Nasıl Alınır
Star Atlas Coin Hangi Borsada
Tesla Coin Hangi Borsada
Sohbet
Parasız Görüntülü Sohbet
A60EB
ReplyDeleteBinance Hesap Açma
Binance Sahibi Kim
Referans Kimliği Nedir
Bitranium Coin Hangi Borsada
Kripto Para Çıkarma
Bitcoin Kazanma
Mexc Borsası Güvenilir mi
Sohbet
Threads Yeniden Paylaş Hilesi
3211C
ReplyDeletekraken
coin nasıl alınır
kraken
probit
kucoin
paribu
paribu
mexc
binance 100 dolar
74C12
ReplyDeletekucoin
bitcoin nasıl üretilir
kucoin
bitrue
kraken
mexc
telegram kripto para kanalları
copy trade nedir
referans kodu binance
7BCD2
ReplyDeleteOctober 2024 Calendar
August 2024 Calendar
May 2024 Calendar
gate io
March 2024 Calendar
February 2024 Calendar
referans kodu
canlı sohbet odaları
telegram en iyi kripto grupları
A721E
ReplyDeletehuobi
4g proxy
kripto para kanalları telegram
mexc
kucoin
canlı sohbet siteleri
bybit
kucoin
en düşük komisyonlu kripto borsası