Changing RavenDB’s Port From 8080 – Windows Service

A few folks have asked how to change the port of which RavenDB is listening (defaults to 8080). It’s easily changed by editing a config file. Below are the steps if you’re running Raven as a Windows service.

  1. Launch Services and stop the RavenDB service. ravenservice
  2. Locate the “Raven.Server.exe.config” file from your install directory of RavenDB. (mine was in f:\ravendb\)
  3. Open this config file and change this line: <add key=”Raven/Port” value=”8080″/> to whatever available port you’d like (I did 8082)
  4. Save the config file.
  5. Start the service back up.

RavenDB should now be listening on your new port. You can test by going to http://localhost:8082  (or whatever hostname you’re using).

Changing RavenDB’s Port From 8080 – Windows Service

Fixing / Removing Invalid Characters from a File Path / Name – c#

Below is a simple method for fixing bad filenames and paths. This uses the character lists from Path.GetInvalidPathChars and Path.GetInvalidFileNameChars (part of System.IO).

You should be able to pass a filename, directory or path. Example, calling these three lines would yield the below:

cleanPath(@"c:\tem|<p\fi<>le.txt")
cleanPath(@"c:\tem|<p\")
cleanPath(@"fi<le.txt")

Returns:

c:\tem-p\fi-le.txt
c:\tem-p\
fi-le.txt

You can also pass a string that’s used to replace the bad characters.

cleanPath(@"c:\tem|<p\fi<>le.txt", string.Empty)

Returns:

c:\temp\file.txt
 private string cleanPath(string toCleanPath, string replaceWith = "-")  
      {  
           //get just the filename - can't use Path.GetFileName since the path might be bad!  
           string[] pathParts = toCleanPath.Split(new char[] { '\\' });  
           string newFileName = pathParts[pathParts.Length - 1];  
           //get just the path  
           string newPath = toCleanPath.Substring(0, toCleanPath.Length - newFileName.Length);   
           //clean bad path chars  
           foreach (char badChar in Path.GetInvalidPathChars())  
           {  
                newPath = newPath.Replace(badChar.ToString(), replaceWith);  
           }  
           //clean bad filename chars  
           foreach (char badChar in Path.GetInvalidFileNameChars())  
           {  
                newFileName = newFileName.Replace(badChar.ToString(), replaceWith);  
           }  
           //remove duplicate "replaceWith" characters. ie: change "test-----file.txt" to "test-file.txt"  
           if (string.IsNullOrWhiteSpace(replaceWith) == false)  
           {  
                newPath = newPath.Replace(replaceWith.ToString() + replaceWith.ToString(), replaceWith.ToString());  
                newFileName = newFileName.Replace(replaceWith.ToString() + replaceWith.ToString(), replaceWith.ToString());  
           }  
           //return new, clean path:  
           return newPath + newFileName;  
      }  

Hope it helps!

Fixing / Removing Invalid Characters from a File Path / Name – c#

How To Install Redis on Windows and Get Started With C#

Dictionary_IconRedis is a key-value store that is fast, sits in memory and runs on many different platforms. Installing on Windows isn’t straightforward from the Redis site, so below are the steps I use to get going.

1. Download the latest .zip from: https://github.com/mythz/redis-windows/tree/master/downloads

2. Extract these files to a folder (I did f:\redis)

3. In a command prompt, run

redis-server.exe redis.windows.conf

(from your directory above).

Redis should now be running on port 6379. You can change this in redis.windows.conf  if you’d like.

4. Start a new Visual Studio Project (I’m using c# / .net 4.5).

5. In Package Manager Console (NuGet) let’s install the client, run:

Install-Package StackExchange.Redis

6. Include using StackExchange.Redis;

7. Now you can use the below to save and get values:

  //setup your connection
  ConnectionMultiplexer redisConn = ConnectionMultiplexer.Connect("localhost");

 //get your db
 IDatabase redDb = redisConn.GetDatabase();

 //save a key & val
 redDb.StringSet("testKey", "test val");

 //get a key & val
 Console.WriteLine(redDb.StringGet("testKey"));

This is just start of what Redis can do, find more at the official Redis site.

How To Install Redis on Windows and Get Started With C#

Simple Single Page / Single User Forms Authentication without DB – C#

simple-loginOn smaller projects, maybe for internal usage, you want to protect a page with a username and password. Below is an easy way you can protect a page (or pages / folders) using a user and pass (kept in the web.config for easy access). This method uses forms authentication but doesn’t require a database or other source for user details since a single user is just stored in the web.config.

You’ll basically need to.

  1. Create a login page (simple .aspx page)
  2. Modify your web.config

 

Let’s start with your web.config. Below are the areas you’ll want to add: Continue reading “Simple Single Page / Single User Forms Authentication without DB – C#”

Simple Single Page / Single User Forms Authentication without DB – C#

Watching & Printing New Files in a Directory – vb & c#

FileWatcherPrinterMonitoring a folder for new files in .net can easily be watched using the FileSystemWatcher in System.IO. You give it a path (ie: C:\toprint) and it will raise an event when a file is created (you can also watch for deleted, renamed and updated files).

Edit: I have new / full code here on GitHub.

I’ve had the need on several occasions to print new files as they are created, so the combination of the FileSystemWatcher and using a process to call the file and print work well together. Below the code to print to the default printer (works great for .pdf files):

c#:

 Process PrintProcess = new Process();
 PrintProcess.StartInfo.CreateNoWindow = false;
 PrintProcess.StartInfo.Verb = "print";
 PrintProcess.StartInfo.FileName = e.FullPath;
 PrintProcess.Start();

vb:

 Dim PrintProcess As New Process
 PrintProcess.StartInfo.CreateNoWindow = False
 PrintProcess.StartInfo.Verb = "print"
 PrintProcess.StartInfo.FileName = e.FullPath
 PrintProcess.Start()

The file watcher uses this syntax:

c#:

FileSystemWatcher fsWatcher = new FileSystemWatcher(txtDirToWatch.Text);
fsWatcher.Created += OnChanged;
 var withFSW = fsWatcher;
 withFSW.EnableRaisingEvents = true;
 withFSW.IncludeSubdirectories = false;
 withFSW.WaitForChanged(WatcherChangeTypes.Created);
 withFSW.Filter = "*.pdf";

vb:

Dim fsWatcher As New FileSystemWatcher(txtDirToWatch.Text)
 
AddHandler fsWatcher.Created, AddressOf OnChanged
With fsWatcher
 .EnableRaisingEvents = True
 .IncludeSubdirectories = False
 .WaitForChanged(WatcherChangeTypes.Created)
 .Filter = "*.pdf"
End With

And the OnChanged Event: c#:

 public void OnChanged(object source, FileSystemEventArgs e)
 {
   //Do something with e.FullPath, etc.
 }

 

vb:

 Public Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
  'Do something with e.FullPath, etc.
 End Sub

 

*I haven’t used this method to print many different formats or tons of files at once, but give it a try!

If you’d like a fully working solution (vb or c#) drop me a line at chris.bitting(at)gmail(dot)com and I’d be glad to send my project and code.

Watching & Printing New Files in a Directory – vb & c#