I have to go through all team drives, folders, and files and the code I have works perfectly fine:
/// <summary>
/// Get All files inside a folder
/// </summary>
/// <param name="service"></param>
/// <param name="folderId"></param>
/// <param name="td"></param>
/// <returns></returns>
private static async Task<Google.Apis.Drive.v3.Data.FileList> GetAllFilesInsideFolder(DriveService service, string folderId, TeamDrive td)
{
string FolderId = folderId;
// Define parameters of request.
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.Corpora = "drive";
listRequest.SupportsAllDrives = true;
listRequest.DriveId = td.Id;
listRequest.PageSize = 100;
listRequest.IncludeItemsFromAllDrives = true;
listRequest.Q = "'" + FolderId + "' in parents and trashed=false";
listRequest.Fields = "nextPageToken, files(*)";
// List files.
Google.Apis.Drive.v3.Data.FileList files = await listRequest.ExecuteAsync();
return files;
}
/// <summary>
/// Gets all folders from an specific team drive
/// </summary>
/// <param name="service"></param>
/// <param name="td"></param>
/// <returns></returns>
private static async Task<Google.Apis.Drive.v3.Data.FileList> GetAllFoldersFromTeamDriveAsync(DriveService service, TeamDrive td)
{
try
{
var request = service.Files.List();
request.Corpora = "drive";
request.SupportsAllDrives = true;
request.DriveId = td.Id;
request.PageSize = 100;
request.IncludeItemsFromAllDrives = true;
request.Q = "mimeType = 'application/vnd.google-apps.folder'";
var response = await request.ExecuteAsync();
return response;
}
catch (Exception ex )
{
Console.WriteLine("GetAllFoldersFromTeamDriveAsync Error: " + ex.Message);
throw;
}
}
/// <summary>
/// Gets all teamdrives
/// </summary>
/// <param name="service"></param>
/// <returns></returns>
private static async Task<Google.Apis.Drive.v3.Data.TeamDriveList> GetAllTeamDrivesAsync(DriveService service)
{
try
{
var request = service.Teamdrives.List();
request.PageSize = 100;
var response = await request.ExecuteAsync();
return response;
}
catch (Exception ex)
{
Console.WriteLine("GetAllTeamDrivesAsync Error" + ex.Message);
throw;
}
}
Main method:
public async Task RunFix()
{
UserCredential credential;
Directory.CreateDirectory(StorageLocation);
var DataStore = new FileDataStore(StorageLocation);
using var stream = new FileStream(CredentialsString, FileMode.Open, FileAccess.Read);
// Please give consent to the Auth/drive scope
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
Username,
CancellationToken.None,
DataStore).Result;
try
{
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
var clientbillCycleGoogleDriveFilesDEV = new TableClient("...");
// Get All team drives
var allTeamDrives = await GetAllTeamDrivesAsync(service);
// We will use a CSV to copy the unmapped files
var csvUnmappedBillCycleFiles = new StringBuilder();
//Iterate over all Team Drives I have access to:
foreach (TeamDrive td in allTeamDrives.TeamDrives)
{
if (td.Name == "XXX")
{
Console.WriteLine("Team Drive: " + td.Name);
Stopwatch sAllFilesInsideFolder = Stopwatch.StartNew();
// Get All Folders inside this team drive
var allFolders = await GetAllFoldersFromTeamDriveAsync(service, td);
foreach (Google.Apis.Drive.v3.Data.File file in allFolders.Files)
{
Console.WriteLine("--Folder-- " + file.Name);
if (file.Name.StartsWith("xyz"))
{
//some business logic
foreach (Google.Apis.Drive.v3.Data.File googlefile in allFilesInsideFolder.Files)
{
//some business logic
}
}
}
}
}
System.IO.File.WriteAllText("csvUnmappedBillCycleFiles.csv", csvUnmappedBillCycleFiles.ToString());
}
catch (Exception ex)
{
Console.WriteLine("Error" + ex.Message);
}
}
The problem is that I cant set PageSize to something above 100.
How can I change this code to actually to go through everything?
Assuming that you are using drives list and files list. Which is just a guess since you haven't included the code GetAllTeamDrivesAsync? GetAllFoldersFromTeamDriveAsync.
You can set the page size to max 1000 on your file.list method.
var request = service.Files.List();
request.PageSize = 1000;
But thats you are still going to need to paginate over the page. I recommend using the pageStreamer. It will loop over each of the rows for you.
var request = service.Files.List();
request.PageSize = 1000;
var pageStreamer =
new Google.Apis.Requests.PageStreamer<Google.Apis.Drive.v3.Data.File, FilesResource.ListRequest,
Google.Apis.Drive.v3.Data.FileList, string>(
(req, token) => request.PageToken = token,
response => response.NextPageToken,
response => response.Files);
// data storage
var all = new Google.Apis.Drive.v3.Data.FileList();
all.Files = new List<Google.Apis.Drive.v3.Data.File>();
// fetching data and adding it to storage
foreach (var result in await pageStreamer.FetchAllAsync(request, CancellationToken.None))
{
all.Files.Add(result);
}
I have a video up that explains how pagestreamer works How to use the nextpagetoken from the files list method in the Google Drive api v3.
You should be able to use pagestreamer with drives list as well.
After search here in the forum i found solution to my question: i have root folder and i want to find the newest file from each directory under the root folder:
public static void FindNewestFile(string path)
{
List<string> list = getNewestFile(path);
foreach (string dir in list)
{
DirectoryInfo directory = new DirectoryInfo(dir);
try
{
FileInfo file = directory.GetFiles("*.*", SearchOption.AllDirectories).OrderByDescending(f => f.LastWriteTime).FirstOrDefault();
if (file != null)
{
// Do things with my file
}
}
catch (UnauthorizedAccessException)
{ }
}
}
private static List<string> getNewestFile(string path)
{
List<string> list = new List<string>();
foreach (string dir in EnumerateFoldersRecursively(path))
{
list.Add(dir);
}
return list;
}
private static IEnumerable<string> EnumerateFoldersRecursively(string root)
{
foreach (var folder in EnumerateFolders(root))
{
yield return folder;
foreach (var subFolder in EnumerateFoldersRecursively(folder))
{
yield return subFolder;
}
}
}
private static IEnumerable<string> EnumerateFolders(string root)
{
WIN32_FIND_DATA findData;
string spec = Path.Combine(root, "*");
using (SafeFindHandle findHandle = FindFirstFile(spec, out findData))
{
if (!findHandle.IsInvalid)
{
do
{
if ((findData.cFileName != ".") && (findData.cFileName != "..")) // Ignore special "." and ".." folders.
{
if ((findData.dwFileAttributes & FileAttributes.Directory) != 0)
{
yield return Path.Combine(root, findData.cFileName);
}
}
}
while (FindNextFile(findHandle, out findData));
}
}
}
My problem is that it bypass the root directory and not return the newest file from this directory
Just slightly alter your code to add this line:
List<string> list = getNewestFile(path);
list.Add(path); //Add current directory to list as well
foreach (string dir in list) //..etc
Should be the easiest fix I'd say.
If you want have the list of all your file you can do this:
string[] filePaths = Directory.GetFiles(#"c:\MyDir\", SearchOption.AllDirectories);
And then you can compare te array with an other older to see if there are new file or if files are delete
Wow, that is a lot of code for something where you can let .Net's BCL classes (and Linq) do the heavy lifting...
This should suffice:
public IEnumerable<FileInfo> GetNewestFilePerDirectory(
string root,
string pattern = "*",
SearchOption searchoption = SearchOption.TopDirectoryOnly
)
{
return new DirectoryInfo(root)
.EnumerateFiles(pattern, searchoption)
.GroupBy(g => g.Directory.FullName)
.Select(s => s.OrderBy(f => f.Name)
.First(f => f.CreationTimeUtc == s.Max(m => m.CreationTimeUtc))
);
}
A simple console-app demonstration / documentation to put the dots on the i:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
private static void Main(string[] args)
{
var newestfiles = GetNewestFilePerDirectory(
#"D:\foo\bar", "*", SearchOption.AllDirectories
);
Console.WriteLine(string.Join("\r\n", newestfiles.Select(f => f.FullName)));
}
/// <summary>
/// Scans a directory (and, optionally, subdirectories) and returns an
/// enumerable of <see cref="FileInfo"/> for the newest file in eacht
/// directory.
/// </summary>
/// <param name="root">
/// A string specifying the path to scan.
/// </param>
/// <param name="pattern">
/// The search string. The default pattern is "*", which returns all files.
/// </param>
/// <param name="searchoption">
/// One of the enumeration values that specifies whether the search operation should
/// include only the current directory or all subdirectories. The default value is
/// <see cref="SearchOption.TopDirectoryOnly"/>.
/// </param>
/// <returns>
/// Returns the newest file, per directory.
/// </returns>
/// <remarks>
/// For directories containing files of the same createtiondate, the first file when
/// sorted alphabetical will be returned.
/// </remarks>
private static IEnumerable<FileInfo> GetNewestFilePerDirectory(
string root,
string pattern = "*",
SearchOption searchoption = SearchOption.TopDirectoryOnly
)
{
return new DirectoryInfo(root)
.EnumerateFiles(pattern, searchoption)
.GroupBy(g => g.Directory.FullName)
.Select(s => s.OrderBy(f => f.Name)
.First(f => f.CreationTimeUtc == s.Max(m => m.CreationTimeUtc))
);
}
}
I am using the Core Service on Tridion 2011. I want to create a folder structure, and then create a component in that structure.
Example:
Path of folder structure: /ABCD/DEFG/aaaaa
If the folder exists, we need not create folder. If it doesn't exist we have to create it and create component in it.
I know how to create the component in a folder having URI.
The following is the code I use when I need to Get or Create Folders with SDL Tridion's CoreService. It's a simple recursive method that checks for the existence of the current folder. If it doesn't exist, it goes into GetOrCreate the parent folder and so on until it finds an existing path. On the way out of the recursion, it simply creates the new Folders relative to their immediate parent.
Note: this method does not check the input folderPath. Rather, it assumes it represents a valid path.
private FolderData GetOrCreateFolder(string folderPath, SessionAwareCoreServiceClient client)
{
ReadOptions readOptions = new ReadOptions();
if (client.IsExistingObject(folderPath))
{
return client.Read(folderPath, readOptions) as FolderData;
}
else
{
int lastSlashIdx = folderPath.LastIndexOf("/");
string newFolder = folderPath.Substring(lastSlashIdx + 1);
string parentFolder = folderPath.Substring(0, lastSlashIdx);
FolderData parentFolderData = GetOrCreateFolder(parentFolder, client);
FolderData newFolderData = client.GetDefaultData(ItemType.Folder, parentFolderData.Id) as FolderData;
newFolderData.Title = newFolder;
return client.Save(newFolderData, readOptions) as FolderData;
}
}
I would use IsExistingObject - passing in the WebDAV URL - to see if the Folder already exists. If it returns false, you can go ahead and create the folder.
Edit: Here's some quick pseudo code...
string parentFolderId = #"/webdav/MyPublication/Building%20Blocks";
var client = GetCoreServiceClient();
if (!client.IsExistingObject(parentFolderId + "/AAA"))
{
var folder = client.GetDefaultData(2, parentFolderId);
folder.Title = "AAA";
client.Save(folder);
// Create the other folders and components here
}
This is what we used on one of our projects to create folders for a path.
static FolderData GetOrCreateFolder(List<string> folders,
FolderData root,
SessionAwareCoreService2010Client client)
{
var filter = new OrganizationalItemItemsFilterData();
filter.ItemTypes = new [] { ItemType.Folder };
var items = client.GetListXml(root.Id, filter).
Elements(TRIDION_NAMESPACE + "Item");
foreach (var element in items)
{
if (folders.Count == 0)
{
break; // break from foreach
}
var titleAttribute = element.Attribute("Title");
var idAttribute = element.Attribute("ID");
if (titleAttribute != null && titleAttribute.Value == folders[0] &&
idAttribute != null)
{
// folder exists
FolderData fd = client.Read(idAttribute.Value,
EXPANDED_READ_OPTIONS) as FolderData;
// We just took care of this guy, remove it to recurse
folders.RemoveAt(0);
return GetOrCreateFolder(folders, fd, client);
}
}
if (folders.Count != 0)
{
//Folder doesn't exist, lets create it and return its folderdata
var newfolder = new FolderData();
newfolder.Title = folders[0];
newfolder.LocationInfo = new LocationInfo {
OrganizationalItem = new LinkToOrganizationalItemData {
IdRef = root.Id
}
};
newfolder.Id = "tcm:0-0-0";
var folder = client.Create(newfolder, EXPANDED_READ_OPTIONS)
as FolderData;
folders.RemoveAt(0);
if (folders.Count > 0)
{
folder = GetOrCreateFolder(folders, folder, client);
}
return folder;
}
return root;
}
So you'd invoke it with something like this:
var root = client.Read("tcm:1-1-2", null) as FolderData;
var pathParts = "/ABCD/DEFG/aaaaa".Trim('/').Split('/').ToList();
var folder = GetOrCreateFolder(pathParts, root, client);
For Create a folder use the following code as sample...
You will have to check if the folder exists of course, this code shows how to create a folder within a folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreWebService.ServiceReference1;
namespace CoreWebService
{
class CoreWebServiceSamples
{
public static void createFolder()
{
string folderWebDavUrl = "/webdav/020%20Content/Building%20Blocks/Content/wstest";
CoreServicesUtil coreServicesUtil = new CoreServicesUtil();
FolderData folderData = coreServicesUtil.getFolderData(folderWebDavUrl);
FolderData folderDataChild = folderData.AddFolderData();
folderDataChild.Title = "childFolder";
folderDataChild = (FolderData)coreServicesUtil.coreServiceClient.Save(folderDataChild, coreServicesUtil.readOptions);
coreServicesUtil.coreServiceClient.Close();
}
}
}
Here is some code for the methods referenced ....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreWebService.ServiceReference1;
using CoreWebService.Properties;
using System.Xml;
using System.Xml.Serialization;
namespace CoreWebService
{
public class CoreServicesUtil
{
public CoreService2010Client coreServiceClient;
public ReadOptions readOptions;
/// <summary>
///
/// </summary>
public CoreServicesUtil()
{
this.coreServiceClient = new CoreService2010Client("basicHttp_2010");
this.readOptions = new ReadOptions();
}
public FolderData getFolderData(string tcmuri)
{
FolderData folderData = (FolderData)coreServiceClient.Read(tcmuri, readOptions);
return folderData;
}
}
public static class CoreServicesItemCreator
{
/**
* <summary>
* Name: AddFolder
* Description: returns a new Folder Data created in the folder Data
* </summary>
**/
public static FolderData AddFolderData(this FolderData folderData)
{
FolderData childFolder = new FolderData();
childFolder.LocationInfo = getLocationInfo(folderData);
childFolder.Id = "tcm:0-0-0";
return childFolder;
}
}
}
Can someone please show me how to determine if a certain file/object exists in a S3 bucket and display a message if it exists or if it does not exist.
Basically I want it to:
1) Check a bucket on my S3 account such as testbucket
2) Inside of that bucket, look to see if there is a file with the prefix test_ (test_file.txt or test_data.txt).
3) If that file exists, then display a MessageBox (or Console message) that the file exists, or that the file does not exist.
Can someone please show me how to do this?
Using the AWSSDK For .Net I Currently do something along the lines of:
public bool Exists(string fileKey, string bucketName)
{
try
{
response = _s3Client.GetObjectMetadata(new GetObjectMetadataRequest()
.WithBucketName(bucketName)
.WithKey(key));
return true;
}
catch (Amazon.S3.AmazonS3Exception ex)
{
if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
return false;
//status wasn't not found, so throw the exception
throw;
}
}
It kinda sucks, but it works for now.
Use the S3FileInfo.Exists method:
using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
{
S3FileInfo s3FileInfo = new Amazon.S3.IO.S3FileInfo(client, "your-bucket-name", "your-file-name");
if (s3FileInfo.Exists)
{
// file exists
}
else
{
// file does not exist
}
}
Not sure if this applies to .NET Framework, but the .NET Core version of AWS SDK (v3) only supports async requests, so I had to use a slightly different solution:
/// <summary>
/// Determines whether a file exists within the specified bucket
/// </summary>
/// <param name="bucket">The name of the bucket to search</param>
/// <param name="filePrefix">Match files that begin with this prefix</param>
/// <returns>True if the file exists</returns>
public async Task<bool> FileExists(string bucket, string filePrefix)
{
// Set this to your S3 region (of course)
var region = Amazon.RegionEndpoint.USEast1;
using (var client = new AmazonS3Client(region))
{
var request = new ListObjectsRequest {
BucketName = bucket,
Prefix = filePrefix,
MaxKeys = 1
};
var response = await client.ListObjectsAsync(request, CancellationToken.None);
return response.S3Objects.Any();
}
}
And, if you want to search a folder:
/// <summary>
/// Determines whether a file exists within the specified folder
/// </summary>
/// <param name="bucket">The name of the bucket to search</param>
/// <param name="folder">The name of the folder to search</param>
/// <param name="filePrefix">Match files that begin with this prefix</param>
/// <returns>True if the file exists</returns>
public async Task<bool> FileExists(string bucket, string folder, string filePrefix)
{
return await FileExists(bucket, $"{folder}/{filePrefix}");
}
Usage:
var testExists = await FileExists("testBucket", "test_");
// or...
var testExistsInFolder = await FileExists("testBucket", "testFolder/testSubFolder", "test_");
This solves it:
List the bucket for existing objects and use a prefix like so.
var request = new ListObjectsRequest()
.WithBucketName(_bucketName)
.WithPrefix(keyPrefix);
var response = _amazonS3Client.ListObjects(request);
var exists = response.S3Objects.Count > 0;
foreach (var obj in response.S3Objects) {
// act
}
I know this question is a few years old but the new SDK handles this beautifully. If anyone is still searching this. You are looking for S3DirectoryInfo Class
using (IAmazonS3 s3Client = new AmazonS3Client(accessKey, secretKey))
{
S3DirectoryInfo s3DirectoryInfo = new Amazon.S3.IO.S3DirectoryInfo(s3Client, "testbucket");
if (s3DirectoryInfo.GetFiles("test*").Any())
{
//file exists -- do something
}
else
{
//file doesn't exist -- do something else
}
}
I know this question is a few years old but the new SDK nowdays handles this in an easier manner.
public async Task<bool> ObjectExistsAsync(string prefix)
{
var response = await _amazonS3.GetAllObjectKeysAsync(_awsS3Configuration.BucketName, prefix, null);
return response.Count > 0;
}
Where _amazonS3 is your IAmazonS3 instance and _awsS3Configuration.BucketName is your bucket name.
You can use your complete key as a prefix.
I used the following code in C# with Amazon S3 version 3.1.5(.net 3.5) to check if the bucket exists:
BasicAWSCredentials credentials = new BasicAWSCredentials("accessKey", "secretKey");
AmazonS3Config configurationAmazon = new AmazonS3Config();
configurationAmazon.RegionEndpoint = S3Region.EU; // or you can use ServiceUrl
AmazonS3Client s3Client = new AmazonS3Client(credentials, configurationAmazon);
S3DirectoryInfo directoryInfo = new S3DirectoryInfo(s3Client, bucketName);
bucketExists = directoryInfo.Exists;// true if the bucket exists in other case false.
I used the followings code(in C# with Amazon S3 version 3.1.5 .net 3.5) the file Exists.
Option 1:
S3FileInfo info = new S3FileInfo(s3Client, "butcketName", "key");
bool fileExists = info.Exists; // true if the key Exists in other case false
Option 2:
ListObjectsRequest request = new ListObjectsRequest();
try
{
request.BucketName = "bucketName";
request.Prefix = "prefix"; // or part of the key
request.MaxKeys = 1; // max limit to find objects
ListObjectsResponse response = s3Client .ListObjects(request);
return response.S3Objects.Count > 0;
}
I'm not familiar with C#, but I use this method from Java (conversion to c# is immediate):
public boolean exists(AmazonS3 s3, String bucket, String key) {
ObjectListing list = s3.listObjects(bucket, key);
return list.getObjectSummaries().size() > 0;
}
my 2 cents
public async Task<bool> DoesKeyExistsAsync(string key)
{
ListObjectsResponse response = null;
try
{
response = await _s3Client.ListObjectsAsync(new ListObjectsRequest { BucketName = _settings.BucketName, Prefix = key });
}
catch (Exception ex)
{
_logger.LogError($"Error while checking key {key}");
return false;
}
return (response?.S3Objects?.Count > 0);
}
s3 = new S3(S3KEY, S3SECRET, false);
res = s3->getObjectInfo(bucketName, filename);
It will return array if file exists
try this one:
NameValueCollection appConfig = ConfigurationManager.AppSettings;
AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(
appConfig["AWSAccessKey"],
appConfig["AWSSecretKey"],
Amazon.RegionEndpoint.USEast1
);
S3DirectoryInfo source = new S3DirectoryInfo(s3Client, "BUCKET_NAME", "Key");
if(source.Exist)
{
//do ur stuff
}
using Amazon;
using Amazon.S3;
using Amazon.S3.IO;
using Amazon.S3.Model;
string accessKey = "xxxxx";
string secretKey = "xxxxx";
string regionEndpoint = "EU-WEST-1";
string bucketName = "Bucket1";
string filePath = "https://Bucket1/users/delivery/file.json"
public bool FileExistsOnS3(string filePath)
{
try
{
Uri myUri = new Uri(filePath);
string absolutePath = myUri.AbsolutePath; // /users/delivery/file.json
string key = absolutePath.Substring(1); // users/delivery/file.json
using(var client = AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey, regionEndpoint))
{
S3FileInfo file = new S3FileInfo(client, bucketName, key);
if (file.Exists)
{
return true;
// custom logic
}
else
{
return false;
// custom logic
}
}
}
catch(AmazonS3Exception ex)
{
return false;
}
}
There is an overload for GetFileSystemInfos
Notice this line has filename.*
var files= s3DirectoryInfo.GetFileSystemInfos("filename.*");
public bool Check()
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("AccessKey", "SecretKey");
using (var client = new AmazonS3Client(awsCredentials, Amazon.RegionEndpoint.USEast1))
{
S3DirectoryInfo s3DirectoryInfo = new S3DirectoryInfo(client, bucketName, "YourFilePath");
var files= s3DirectoryInfo.GetFileSystemInfos("filename.*");
if(files.Any())
{
//fles exists
}
}
}
Is there any class in the .NET framework that can read/write standard .ini files:
[Section]
<keyname>=<value>
...
Delphi has the TIniFile component and I want to know if there is anything similar for C#?
Preface
Firstly, read this MSDN blog post on the limitations of INI files. If it suits your needs, read on.
This is a concise implementation I wrote, utilising the original Windows P/Invoke, so it is supported by all versions of Windows with .NET installed, (i.e. Windows 98 - Windows 11). I hereby release it into the public domain - you're free to use it commercially without attribution.
The tiny class
Add a new class called IniFile.cs to your project:
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
// Change this to match your program's normal namespace
namespace MyProg
{
class IniFile // revision 11
{
string Path;
string EXE = Assembly.GetExecutingAssembly().GetName().Name;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);
public IniFile(string IniPath = null)
{
Path = new FileInfo(IniPath ?? EXE + ".ini").FullName;
}
public string Read(string Key, string Section = null)
{
var RetVal = new StringBuilder(255);
GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
return RetVal.ToString();
}
public void Write(string Key, string Value, string Section = null)
{
WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
}
public void DeleteKey(string Key, string Section = null)
{
Write(Key, null, Section ?? EXE);
}
public void DeleteSection(string Section = null)
{
Write(null, null, Section ?? EXE);
}
public bool KeyExists(string Key, string Section = null)
{
return Read(Key, Section).Length > 0;
}
}
}
How to use it
Open the INI file in one of the 3 following ways:
// Creates or loads an INI file in the same directory as your executable
// named EXE.ini (where EXE is the name of your executable)
var MyIni = new IniFile();
// Or specify a specific name in the current dir
var MyIni = new IniFile("Settings.ini");
// Or specify a specific name in a specific dir
var MyIni = new IniFile(#"C:\Settings.ini");
You can write some values like so:
MyIni.Write("DefaultVolume", "100");
MyIni.Write("HomePage", "http://www.google.com");
To create a file like this:
[MyProg]
DefaultVolume=100
HomePage=http://www.google.com
To read the values out of the INI file:
var DefaultVolume = MyIni.Read("DefaultVolume");
var HomePage = MyIni.Read("HomePage");
Optionally, you can set [Section]'s:
MyIni.Write("DefaultVolume", "100", "Audio");
MyIni.Write("HomePage", "http://www.google.com", "Web");
To create a file like this:
[Audio]
DefaultVolume=100
[Web]
HomePage=http://www.google.com
You can also check for the existence of a key like so:
if(!MyIni.KeyExists("DefaultVolume", "Audio"))
{
MyIni.Write("DefaultVolume", "100", "Audio");
}
You can delete a key like so:
MyIni.DeleteKey("DefaultVolume", "Audio");
You can also delete a whole section (including all keys) like so:
MyIni.DeleteSection("Web");
Please feel free to comment with any improvements!
The creators of the .NET framework want you to use XML-based config files, rather than INI files. So no, there is no built-in mechanism for reading them.
There are third party solutions available, though.
INI handlers can be obtained as NuGet packages, such as INI Parser.
You can write your own INI handler, which is the old-school, laborious way. It gives you more control over the implementation, which you can use for bad or good. See e.g. an INI file handling class using C#, P/Invoke and Win32.
This article on CodeProject "An INI file handling class using C#" should help.
The author created a C# class "Ini" which exposes two functions from KERNEL32.dll. These functions are: WritePrivateProfileString and GetPrivateProfileString. You will need two namespaces: System.Runtime.InteropServices and System.Text.
Steps to use the Ini class
In your project namespace definition add
using INI;
Create a INIFile like this
INIFile ini = new INIFile("C:\\test.ini");
Use IniWriteValue to write a new value to a specific key in a section or use IniReadValue to read a value FROM a key in a specific Section.
Note: if you're beginning from scratch, you could read this MSDN article: How to: Add Application Configuration Files to C# Projects. It's a better way for configuring your application.
I found this simple implementation:
http://bytes.com/topic/net/insights/797169-reading-parsing-ini-file-c
Works well for what I need.
Here is how you use it:
public class TestParser
{
public static void Main()
{
IniParser parser = new IniParser(#"C:\test.ini");
String newMessage;
newMessage = parser.GetSetting("appsettings", "msgpart1");
newMessage += parser.GetSetting("appsettings", "msgpart2");
newMessage += parser.GetSetting("punctuation", "ex");
//Returns "Hello World!"
Console.WriteLine(newMessage);
Console.ReadLine();
}
}
Here is the code:
using System;
using System.IO;
using System.Collections;
public class IniParser
{
private Hashtable keyPairs = new Hashtable();
private String iniFilePath;
private struct SectionPair
{
public String Section;
public String Key;
}
/// <summary>
/// Opens the INI file at the given path and enumerates the values in the IniParser.
/// </summary>
/// <param name="iniPath">Full path to INI file.</param>
public IniParser(String iniPath)
{
TextReader iniFile = null;
String strLine = null;
String currentRoot = null;
String[] keyPair = null;
iniFilePath = iniPath;
if (File.Exists(iniPath))
{
try
{
iniFile = new StreamReader(iniPath);
strLine = iniFile.ReadLine();
while (strLine != null)
{
strLine = strLine.Trim().ToUpper();
if (strLine != "")
{
if (strLine.StartsWith("[") && strLine.EndsWith("]"))
{
currentRoot = strLine.Substring(1, strLine.Length - 2);
}
else
{
keyPair = strLine.Split(new char[] { '=' }, 2);
SectionPair sectionPair;
String value = null;
if (currentRoot == null)
currentRoot = "ROOT";
sectionPair.Section = currentRoot;
sectionPair.Key = keyPair[0];
if (keyPair.Length > 1)
value = keyPair[1];
keyPairs.Add(sectionPair, value);
}
}
strLine = iniFile.ReadLine();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (iniFile != null)
iniFile.Close();
}
}
else
throw new FileNotFoundException("Unable to locate " + iniPath);
}
/// <summary>
/// Returns the value for the given section, key pair.
/// </summary>
/// <param name="sectionName">Section name.</param>
/// <param name="settingName">Key name.</param>
public String GetSetting(String sectionName, String settingName)
{
SectionPair sectionPair;
sectionPair.Section = sectionName.ToUpper();
sectionPair.Key = settingName.ToUpper();
return (String)keyPairs[sectionPair];
}
/// <summary>
/// Enumerates all lines for given section.
/// </summary>
/// <param name="sectionName">Section to enum.</param>
public String[] EnumSection(String sectionName)
{
ArrayList tmpArray = new ArrayList();
foreach (SectionPair pair in keyPairs.Keys)
{
if (pair.Section == sectionName.ToUpper())
tmpArray.Add(pair.Key);
}
return (String[])tmpArray.ToArray(typeof(String));
}
/// <summary>
/// Adds or replaces a setting to the table to be saved.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
/// <param name="settingValue">Value of key.</param>
public void AddSetting(String sectionName, String settingName, String settingValue)
{
SectionPair sectionPair;
sectionPair.Section = sectionName.ToUpper();
sectionPair.Key = settingName.ToUpper();
if (keyPairs.ContainsKey(sectionPair))
keyPairs.Remove(sectionPair);
keyPairs.Add(sectionPair, settingValue);
}
/// <summary>
/// Adds or replaces a setting to the table to be saved with a null value.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
public void AddSetting(String sectionName, String settingName)
{
AddSetting(sectionName, settingName, null);
}
/// <summary>
/// Remove a setting.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
public void DeleteSetting(String sectionName, String settingName)
{
SectionPair sectionPair;
sectionPair.Section = sectionName.ToUpper();
sectionPair.Key = settingName.ToUpper();
if (keyPairs.ContainsKey(sectionPair))
keyPairs.Remove(sectionPair);
}
/// <summary>
/// Save settings to new file.
/// </summary>
/// <param name="newFilePath">New file path.</param>
public void SaveSettings(String newFilePath)
{
ArrayList sections = new ArrayList();
String tmpValue = "";
String strToSave = "";
foreach (SectionPair sectionPair in keyPairs.Keys)
{
if (!sections.Contains(sectionPair.Section))
sections.Add(sectionPair.Section);
}
foreach (String section in sections)
{
strToSave += ("[" + section + "]\r\n");
foreach (SectionPair sectionPair in keyPairs.Keys)
{
if (sectionPair.Section == section)
{
tmpValue = (String)keyPairs[sectionPair];
if (tmpValue != null)
tmpValue = "=" + tmpValue;
strToSave += (sectionPair.Key + tmpValue + "\r\n");
}
}
strToSave += "\r\n";
}
try
{
TextWriter tw = new StreamWriter(newFilePath);
tw.Write(strToSave);
tw.Close();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Save settings back to ini file.
/// </summary>
public void SaveSettings()
{
SaveSettings(iniFilePath);
}
}
The code in joerage's answer is inspiring.
Unfortunately, it changes the character casing of the keys and does not handle comments. So I wrote something that should be robust enough to read (only) very dirty INI files and allows to retrieve keys as they are.
It uses some LINQ, a nested case insensitive string dictionary to store sections, keys and values, and read the file in one go.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class IniReader
{
Dictionary<string, Dictionary<string, string>> ini = new Dictionary<string, Dictionary<string, string>>(StringComparer.InvariantCultureIgnoreCase);
public IniReader(string file)
{
var txt = File.ReadAllText(file);
Dictionary<string, string> currentSection = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
ini[""] = currentSection;
foreach(var line in txt.Split(new[]{"\n"}, StringSplitOptions.RemoveEmptyEntries)
.Where(t => !string.IsNullOrWhiteSpace(t))
.Select(t => t.Trim()))
{
if (line.StartsWith(";"))
continue;
if (line.StartsWith("[") && line.EndsWith("]"))
{
currentSection = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
ini[line.Substring(1, line.LastIndexOf("]") - 1)] = currentSection;
continue;
}
var idx = line.IndexOf("=");
if (idx == -1)
currentSection[line] = "";
else
currentSection[line.Substring(0, idx)] = line.Substring(idx + 1);
}
}
public string GetValue(string key)
{
return GetValue(key, "", "");
}
public string GetValue(string key, string section)
{
return GetValue(key, section, "");
}
public string GetValue(string key, string section, string #default)
{
if (!ini.ContainsKey(section))
return #default;
if (!ini[section].ContainsKey(key))
return #default;
return ini[section][key];
}
public string[] GetKeys(string section)
{
if (!ini.ContainsKey(section))
return new string[0];
return ini[section].Keys.ToArray();
}
public string[] GetSections()
{
return ini.Keys.Where(t => t != "").ToArray();
}
}
I want to introduce an IniParser library I've created completely in c#, so it contains no dependencies in any OS, which makes it Mono compatible. Open Source with MIT license -so it can be used in any code.
You can check out the source in GitHub, and it is also available as a NuGet package
It's heavily configurable, and really simple to use.
Sorry for the shameless plug but I hope it can be of help of anyone revisiting this answer.
If you only need read access and not write access and you are using the Microsoft.Extensions.Confiuration (comes bundled in by default with ASP.NET Core but works with regular programs too) you can use the NuGet package Microsoft.Extensions.Configuration.Ini to import ini files in to your configuration settings.
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddIniFile("SomeConfig.ini", optional: false);
Configuration = builder.Build();
}
PeanutButter.INI is a Nuget-packaged class for INI files manipulation. It supports read/write, including comments – your comments are preserved on write. It appears to be reasonably popular, is tested and easy to use. It's also totally free and open-source.
Disclaimer: I am the author of PeanutButter.INI.
Usually, when you create applications using C# and the .NET framework, you will not use INI files. It is more common to store settings in an XML-based configuration file or in the registry.
However, if your software shares settings with a legacy application it may be easier to use its configuration file, rather than duplicating the information elsewhere.
The .NET framework does not support the use of INI files directly. However, you can use Windows API functions with Platform Invocation Services (P/Invoke) to write to and read from the files. In this link we create a class that represents INI files and uses Windows API functions to manipulate them.
Please go through the following link.
Reading and Writing INI Files
If you want just a simple reader without sections and any other dlls here is simple solution:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tool
{
public class Config
{
Dictionary <string, string> values;
public Config (string path)
{
values = File.ReadLines(path)
.Where(line => (!String.IsNullOrWhiteSpace(line) && !line.StartsWith("#")))
.Select(line => line.Split(new char[] { '=' }, 2, 0))
.ToDictionary(parts => parts[0].Trim(), parts => parts.Length>1?parts[1].Trim():null);
}
public string Value (string name, string value=null)
{
if (values!=null && values.ContainsKey(name))
{
return values[name];
}
return value;
}
}
}
Usage sample:
file = new Tool.Config (Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\config.ini");
command = file.Value ("command");
action = file.Value ("action");
string value;
//second parameter is default value if no key found with this name
value = file.Value("debug","true");
this.debug = (value.ToLower()=="true" || value== "1");
value = file.Value("plain", "false");
this.plain = (value.ToLower() == "true" || value == "1");
Config file content meanwhile (as you see supports # symbol for line comment):
#command to run
command = php
#default script
action = index.php
#debug mode
#debug = true
#plain text mode
#plain = false
#icon = favico.ico
Try this method:
public static Dictionary<string, string> ParseIniDataWithSections(string[] iniData)
{
var dict = new Dictionary<string, string>();
var rows = iniData.Where(t =>
!String.IsNullOrEmpty(t.Trim()) && !t.StartsWith(";") && (t.Contains('[') || t.Contains('=')));
if (rows == null || rows.Count() == 0) return dict;
string section = "";
foreach (string row in rows)
{
string rw = row.TrimStart();
if (rw.StartsWith("["))
section = rw.TrimStart('[').TrimEnd(']');
else
{
int index = rw.IndexOf('=');
dict[section + "-" + rw.Substring(0, index).Trim()] = rw.Substring(index+1).Trim().Trim('"');
}
}
return dict;
}
It creates the dictionary where the key is "-". You can load it like this:
var dict = ParseIniDataWithSections(File.ReadAllLines(fileName));
I'm late to join the party, but I had the same issue today and I've written the following implementation:
using System.Text.RegularExpressions;
static bool match(this string str, string pat, out Match m) =>
(m = Regex.Match(str, pat, RegexOptions.IgnoreCase)).Success;
static void Main()
{
Dictionary<string, Dictionary<string, string>> ini = new Dictionary<string, Dictionary<string, string>>();
string section = "";
foreach (string line in File.ReadAllLines(.........)) // read from file
{
string ln = (line.Contains('#') ? line.Remove(line.IndexOf('#')) : line).Trim();
if (ln.match(#"^[ \t]*\[(?<sec>[\w\-]+)\]", out Match m))
section = m.Groups["sec"].ToString();
else if (ln.match(#"^[ \t]*(?<prop>[\w\-]+)\=(?<val>.*)", out m))
{
if (!ini.ContainsKey(section))
ini[section] = new Dictionary<string, string>();
ini[section][m.Groups["prop"].ToString()] = m.Groups["val"].ToString();
}
}
// access the ini file as follows:
string content = ini["section"]["property"];
}
It must be noted, that this implementation does not handle sections or properties which are not found.
To achieve this, you should extend the Dictionary<,>-class to handle unfound keys.
To serialize an instance of Dictionary<string, Dictionary<string, string>> to an .ini-file, I use the following code:
string targetpath = .........;
Dictionary<string, Dictionary<string, string>> ini = ........;
StringBuilder sb = new StringBuilder();
foreach (string section in ini.Keys)
{
sb.AppendLine($"[{section}]");
foreach (string property in ini[section].Keys)
sb.AppendLine($"{property}={ini[section][property]");
}
File.WriteAllText(targetpath, sb.ToString());
There is an Ini Parser available in CommonLibrary.NET
This has various very convenient overloads for getting sections/values and is very light weight.
Here is my own version, using regular expressions. This code assumes that each section name is unique - if however this is not true - it makes sense to replace Dictionary with List. This function supports .ini file commenting, starting from ';' character. Section starts normally [section], and key value pairs also comes normally "key = value". Same assumption as for sections - key name is unique.
/// <summary>
/// Loads .ini file into dictionary.
/// </summary>
public static Dictionary<String, Dictionary<String, String>> loadIni(String file)
{
Dictionary<String, Dictionary<String, String>> d = new Dictionary<string, Dictionary<string, string>>();
String ini = File.ReadAllText(file);
// Remove comments, preserve linefeeds, if end-user needs to count line number.
ini = Regex.Replace(ini, #"^\s*;.*$", "", RegexOptions.Multiline);
// Pick up all lines from first section to another section
foreach (Match m in Regex.Matches(ini, "(^|[\r\n])\\[([^\r\n]*)\\][\r\n]+(.*?)(\\[([^\r\n]*)\\][\r\n]+|$)", RegexOptions.Singleline))
{
String sectionName = m.Groups[2].Value;
Dictionary<String, String> lines = new Dictionary<String, String>();
// Pick up "key = value" kind of syntax.
foreach (Match l in Regex.Matches(ini, #"^\s*(.*?)\s*=\s*(.*?)\s*$", RegexOptions.Multiline))
{
String key = l.Groups[1].Value;
String value = l.Groups[2].Value;
// Open up quotation if any.
value = Regex.Replace(value, "^\"(.*)\"$", "$1");
if (!lines.ContainsKey(key))
lines[key] = value;
}
if (!d.ContainsKey(sectionName))
d[sectionName] = lines;
}
return d;
}
If you don't need bells and whistles (ie sections) here's a one liner:
List<(string, string)> ini = File.ReadLines(filename)
.Select(s => {
var spl = s.Split('=', 2);
return spl.Length == 2 ? (spl[0], spl[1]) : (s, "");
})
.Select(vt => (vt.Item1.Trim(), vt.Item2.Trim()))
.Where(vt => vt.Item1 != "")
.ToList();
To write:
File.WriteAllLines(filename, ini.Select(vt => $"{vt.Item1}={vt.Item2}"));
(if you don't care about duplicates use .ToDictionary() instead of .ToList() for easier access)
Here is my class, works like a charm :
public static class IniFileManager
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal,
int size, string filePath);
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileSection(string lpAppName,
byte[] lpszReturnBuffer, int nSize, string lpFileName);
/// <summary>
/// Write Data to the INI File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// Section name
/// <PARAM name="Key"></PARAM>
/// Key Name
/// <PARAM name="Value"></PARAM>
/// Value Name
public static void IniWriteValue(string sPath,string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, sPath);
}
/// <summary>
/// Read Data Value From the Ini File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// <PARAM name="Key"></PARAM>
/// <PARAM name="Path"></PARAM>
/// <returns></returns>
public static string IniReadValue(string sPath,string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,
255, sPath);
return temp.ToString();
}
}
The use is obviouse since its a static class, just call IniFileManager.IniWriteValue for readsing a section or IniFileManager.IniReadValue for reading a section.
You should read and write data from xml files since you can save a whole object to xml and also you can populate a object from a saved xml. It is better an easy to manipulate objects.
Here is how to do it:
Write Object Data to an XML File: https://msdn.microsoft.com/en-us/library/ms172873.aspx
Read Object Data from an XML File: https://msdn.microsoft.com/en-us/library/ms172872.aspx